I have a daily process which generates some zip files out of files that are being created by other processes. I need to create a daily log file which indicates the timestamps the contents of one specific file of each zip file that is found.
I created the following batch script which seemed to work yesterday on my test system, but not anymore today, no idea why…
set VersionDirectory=C:\Test\VersionX\
set ResultOutputFile=C:\Test\LogFile.txt
for /f %%f in ('dir /b %VersionDirectory%\Installable\Packages\pattern*.zip') do (
mkdir %temp%\%%f\
unzip -extract -dir %VersionDirectory%\Installable\Packages\%%f %temp%\%%f\ > nul
for %%a in (%temp%\%%f\InstallScript.xml) do set InstallScriptXMLDate=%%~ta
rmdir /s /q %temp%\%%f\
echo %%f [package from %InstallScriptXMLDate%] >> %ResultOutputFile%
)
Short summary of what this file is supposed to do:
- Loop through each pattern*.zip file in C:\Test\VersionX\ directory
- unzip this file to the %temp%\%%f directory (where %%f is the filename)
- Get the timestamp of the %temp%\%%f\InstallScript.xml and put it in the %InstallScriptXMLDate% variable
- Delete the %temp%\%%f directory
- Echo the filename (%%f) and timestamp (%InstallScriptXMLDate%) into the log file
As of now the log file just contains the filenames, followed by the string ‘[package from ]’ string, but missing the actual date timestamp
The unzipping and removing of the zip files is working flawlessly, it’s just the timestamp that’s not being set.
You are setting a variable and using it in the same block. This cannot work in
cmdbecause environment variables are expanded when a statement is parsed not when it’s executed. So when the loop is run all variables have already been replaced with the values they had before the loop.Put
at the start of your batch and use
!InstallScriptXmlDate!instead of%InstallScriptXmlDate%.Another note:
foris perfectly capable of iterating over files by itself, you almost never need to iterate overdir /boutput withfor /f. In fact, it can introduce problems that can be avoided with