Solved Why is my code for my batch file not working?

August 12, 2014 at 05:36:07
Specs: Windows 7
I am not even a novice when it comes to batch scripts. I have files that have the same name but different extensions. Extensions are"


I am trying to create a batch that moves the pdf when it exist to another folder and move the other three extensions to another folder. This is what I have

@echo off

for /f %%a in (*.tif) do
if exist "%%~na.pdf, %%~na.acc," (
move /-y "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\Misti\%%~na.pdf" "P:\1_Previous Batch\Batch_20140811\PROD\PDF\5_FQC\1_Pending")

for /f %%a in (*.tif) do
if exist "%%~na.pdf, %%~na.acc," (
move /-y "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\Misti\" "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\1_Done")

for /f %%a in (*.tif) do
if exist "%%~na.pdf, %%~na.acc," (
move /-y "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\Misti\%%~na.acc" "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\1_Done")

for /f %%a in (*.tif) do
if exist "%%~na.pdf, %%~na.acc," (
move /-y "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\Misti\%%~na.tif" "P:\1_Previous Batch\Batch_20140811\PROD\PDF\4_Verify\1_Done")

See More: Why is my code for my batch file not working?

Report •

August 12, 2014 at 12:52:28
1: if exist "%%~na.pdf, %%~na.acc," (

As far as I know, this doesn't work - syntactically not supported, and it's not necessary anyway. (why test for the existance of files before moving them? If the file is not existant, it won't get moved anyhow.)

2: for /f %%a in (*.tif) do
I believe you'er confused here. The intention does not call for the "/f" clause in the "for" statement.

3: it's a matter of personal preference, but I prefer to use a single variable for long paths. It makes the batch more readable, easier to debug, and easier to modify (if you wanted to alter the path, f/e, you only need to alter one location, not multiple).

Analysis of intent: all tif-matching PDFs to "pending", all tifs and tif-matching ACCS and PROs to "done".
Bearing all this in consideration, here's my spin:
::======== begin batch re-write
@echo off
set base1=P:\1_Previous Batch\Batch_20140811\PROD\PDF
set source=%base1%\4_Verify\Misti
set pdf_pending=%base1%\5_FQC\1_Pending
set done=%base%\4_Verify\1_Done
:: not sure what path you intended. Making assumptions which might be wrong
for /f %%a in ('dir /b *.tif') do (
move /-y %source%\%%~na.pdf %pdf_pending% 2>nul
move /-y %source%\ %done% 2>nul
move /-y %source%\%%~na.acc %done% 2>nujl
move /-y %source%\%%~na.tif %done%
::---------- end batch
I used "for /f" with "dir /b *.tif", instead of simply "for %%a in (*.tif)"to dodge a potential recursive implosion, or whatever you call it when you alter directory contents which are the basis of a loop. The "2>nul" sends any errors into the void, instead of cluttering your screen for any non-existant files.

Report •

August 13, 2014 at 06:58:45
Do need to edit comments before trying this? I saved my batch file and tried it but nothing happens.

Report •

August 13, 2014 at 12:31:15
✔ Best Answer
No, it's not the comments. But the batch, as it's written, has to be run from within the same directory that the TIF files are in. Otherwise, you can put a "cd" into the batch to go to the directory, or fully qualify the path of the TIFs:
... ('dir /b %source%\*.tif') do (...
check back and let me know...

Report •
Related Solutions

Ask Question