I'm having some difficulty getting my head around variables in my batch file for loops. When I think I have it down, I find an example where it doesn't work as I expect and I feel I've been going in circles with this...
I've written my entire bulk of code incase it makes a difference, basically I have an archive folder with many subfolders and I want to check if my upload data (which will be moved to the archive folder at the end of the script - to be written when I get this part working) has already been processed. If so, move to a Duplicates folder with the date:
@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set localDT=%%j
set localYYYY=%localDT:~0,4%
set localMM=%localDT:~4,2%
set localDD=%localDT:~6,2%
set localDate=%localYYYY%-%localMM%-%localDD%
set localTime=%localDT:~8,2%:%localDT:~10,2%
for %%f in ("C:\Test\837Store\*.837") DO if exist /R "C:\Test\Archive\%%~nxf" (
set VariableTemp=%%~nxf
echo %VariableTemp%
IF NOT EXIST "C:\Test\Archive\Duplicates" mkdir "C:\Test\Archive\Duplicates"
IF NOT EXIST "C:\Test\Archive\Duplicates\%localYYYY%" mkdir "C:\Test\Archive\Duplicates\%localYYYY%"
IF NOT EXIST "C:\Test\Archive\Duplicates\%localYYYY%\%localMM%" mkdir "C:\Test\Archive\Duplicates\%localYYYY%\%localMM%"
copy "%%~nxf" "C:\Test\Archive\Duplicates\%localYYYY%\%localMM%"
echo %localDate% %localTime% Duplicate claims file: %%~dpnxf File moved to archive >> "C:\Test\Log\Error.log"
)
pause
My problem is, the variable %%f doesn't seem to be set through the loop. The mkdir commands are run, but I get a "file not found" error when we get to the copy. To try and see what was happening, I included the VariableTemp and set as per the code, expecting to see the name and extension of the file. Instead, my echo is printing "%~nxf"
Can anyone help me understand how these variables are working? Why am I not seeing the file names from my 837Store?
Just for additional information, I have the following 2 files in C:\Test\837Store:
1.837 2.837
I have the same 2 files in C:\Test\Archive\2013\08 and so would expect them to be moved from 837Store to the relevant Duplicates folder.
Thanks.
"%~nxf"as the value ofVariableTempindicates that a previous run had setVariableTempto this value. Unlike most languages, batch inherits variables from prior programs run in the samecmdsession. Any change made by a batch program remains in place until specifically changed again - UNLESS the change is made between asetlocaland anendlocal(or end-of-file/program) at which time the environment is restored to its state when thesetlocalwas executed.IF NOT EXIST "dirname"certainly works, butmd "dirname" 2>nulwill make a directory if it does not already exist, the2>nuldumps the 'it already exists' message. There's little point in putting thisINSIDEthe loop, since the target directory(s) names don't change within the loop. Themd/mkdir(they're synonymous) should be moved outside of the loop - before theFOR- done once, not (potentially) many times. Finally,IF EXIST /Ris not nominally valid syntax. What is it intended to do?