I created a script to perform a number of actions on specific files. There are parameters to perform those specific actions to files with name %filename%. I'd like to be able to add more than one file at once. Any ideas?

set /p filename=Enter database to replace:

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.CHR" "\\enterprise\INFACTPR\CLIENT"

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.HED" "\\enterprise\INFACTPR\CLIENT"

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.PRD" "\\enterprise\INFACTPR\CLIENT"

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.IDX" "\\enterprise\INFACTPR\CLIENT"

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.INF" "\\enterprise\INFACTPR\CLIENT"

move /y "\\enterprise\INFACTCP\CLIENT\%filename%.TAD" "\\enterprise\INFACTPR\CLIENT"

del \\enterprise\INFACT\INFACTCP\LOG\%filename%.CHK

copy "\\enterprise\INFACTPR\CLIENT\%filename%.HED" "\\enterprise\INFACTPR\CLIENT\%~1.NEW"

I'd like to be able to add more than one value to be replaced by %filename%. Let's suppose that when I'm prompted Enter the database to replace, I could add ABC, ABC1 and the process would be performed individually for all files named ABC and ABC1.

2 Answers

Stephan On

use a for loop to disassemble your string:

set /p "filename=database(s)? "
for %%a in (%filename%) do (
  echo "\\enterprise\INFACTCP\CLIENT\%%~a.CHR" "\\enterprise\INFACTPR\CLIENT"

Try with hello world or simple "two words"

michael_heath On
@echo off

set "clientdir=\\enterprise\INFACTCP\CLIENT"
set "newfilename=%~1"

set /p "fileset=Enter database to replace: " || exit /b 0

for %%A in (%fileset%) do (
    for %%B in (CHR HED PRD IDX INF TAD) do (
        if "%%~B" == "HED" if defined newfilename (
            copy "%clientdir%\%%~A.%%~B" "%clientdir%\%newfilename%.NEW"
        move /y "%clientdir%\%%~A.%%~B" "%clientdir%\"
    del "\\enterprise\INFACT\INFACTCP\LOG\%%~A.CHK"

At the input prompt, you can enter names i.e. card "my book" chain key. That is the names of card, my book, chain and key with the double quotes stripped from them. If nothing is entered, the script will exit, by the use of exit /b 0.

The 1st for loop will set each name to %%A. The 2nd nested for loop will set %%B to each extension. The extension HED needs to be copied before it is moved, else it will not exist for the copy, so it is copied before any move operation happens. The filename with CHK extension will be deleted. The loops will continue until the file set has all been done.

View for /? about a file set (set) which is at the very top.