How to use command line to display filenames in a directory but exclude 1st 3 characters

I am trying to write some vba code that sends a line of code to the command line and executes it. I have this part, but I need help to get the actual code to work.

I want to list all the files in a specific folder that are the file extension .doc

, but I want to exclude the first three characters of the file name to be printed to the output text file. (Note: I am using vba because it is one of several different commands I would like to receive in a single vba macro and I cannot use the b / c batch files they locked on my system so I would like to work directly with command line)

The following code works and gives me filenames without file extension (i.e. ABC201704.doc

will return as ABC201704

)

*%comspec% /c for %i in (C:\Test\ABC*.doc) do @echo %~ni >> C:\Test\Output.txt*

      

However, I don't know how to change this so that it doesn't include the first 3 characters (i.e. I would like to return 201704

instead ABC201704

).

Any help would be greatly appreciated! I tried using the following link, but I couldn't figure out how to do it for my situation.

+3


source to share


1 answer


Not tested:

@echo off
setlocal enableDelayedExpansion

for %%a in ("C:\Test\ABC*.doc") do (
   set docname=%%~nxa
   echo !docname:~3!
) 

      



On the command line:

cmd /v:on  /c "for %a in  ("C:\Test\ABC*.doc") do set  docname=%~nxa & echo !docname:~3!"

      

+1


source







All Articles