DOS Batch File Variable Modifier Returns Blank - variables

I have a DOS batch file that will create a report listing files contained within a folder tree. The code below produces the desired output for over 115,000 files. However, 13 records are produced with blank date/time and file size. When I manually execute the DIR command (without the /b option), the desired file information is presented. Can this be corrected without adding considerable workaround code?
FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA >> test.txt
)

(FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
if exists "%%~A" ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA
)) >> test.txt
The main reason for not obtaining a date/filesize is that the file can not be found.
How does your code work?
The for /f starts a separate cmd instance that runs the dir command.
When all the data has been retrieved and loaded into memory (that is, the cmd/dir command finished), then the for will start to iterate over the retrieved lines. Some time have passed between the information retrieval and the information processing.
In this time, "maybe" the problematic files have been moved/deleted/renamed and they can no be accessed to retrieve their properties. So, first check if the file still exists
The aditional parenthesis and redirection change are to avoid having to open the target file for each echo operation. This way, the file is opened at the start of the for command and closed at the end.

Related

How do I use command prompt to identify derivative generation drop off?

I have one folder of approximately 7500 pdfs and a second folder with approximately 7300 tiff derivatives. Somewhere over the past 4 days of processing, intermittent tiff derivative generation failure occurred. How do I identify which files dropped off?
So far Ive tried:
diff -rq folder_pdfs folder_tiffs
However that reports all files as different given the difference in file extensions.
How do I identify which files dropped off?
Use the following batch file.
MissingFiles.cmd:
#echo off
setlocal
for /f %%f in ('dir /b folder_pdfs') do (
if not exist folder_tiffs\%%~nf.tiff (
echo folder_tiffs\%%~nf.tiff
)
)>>MissingFiles.txt
endlocal
Notes:
MissingFiles.txt will contain the list of missing files.
Example:
F:\test>dir /b folder_pdfs
1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
F:\test>dir /b folder_tiffs
1.tiff
3.tiff
5.tiff
F:\test>MissingFiles.cmd
F:\test>type MissingFiles.txt
folder_tiffs\2.tiff
folder_tiffs\4.tiff
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.
parameters - A command line argument (or parameter) is any value passed into a batch script.

Renaming files using the parent folder/directory name as a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a batch script that will rename all the documents in all sub-folder with the parent folder name and then the original file name. For example:
There are 200 folders with unique 4 digit names, all the folders contain a folder called "Data Sheets". I would like to rename all the documents in the Data Sheets folder to include the 4 digit unique id name. So essentially I want the documents to be renamed from data sheet.doc to 1234 data sheet.doc
There are many batch scripts out there that do something very close to what I am trying to do but I have been unable to modify them to do exactly what I need. Any help would be appreciated. This is what I have been trying of similar versions trying to get it to work.
#echo off
for /r %%a in ("S:\Fiscal Yr. 2015\*\*.doc") do (
for /d %%d in (%CD%) do (
set newname=%%~nd%~x1
ren "%%~a" "!newname!%%~Xa"
echo media file in %%~fa renamed to "!newname!%%~Xa"
)
)
Zach
To clarify - this will process every .doc file in any folder called "Data Sheets" and rename the files to add the parent folder-name to "Data Sheets" to the start of the .doc files.
This will echo the rename command for you to confirm that it is correct, before removing the first echo statement.
#echo off
for /r "S:\Fiscal Yr. 2015" %%a in (*.doc) do (
for /f "delims=" %%b in ("%%~pa\.") do (
if /i "%%~nxb"=="Data Sheets" for /f "delims=" %%c in ("%%~pb\.") do (
echo ren "%%a" "%%~nxc %%~nxa"
echo media file "%%a" renamed to "%%~nxc %%~nxa"
)
)
)
pause
Here's a simple example of how you can rename all files in an entire folder structure, including all sub-folders:
#echo off
setlocal ENABLEDELAYEDEXPANSION
cd /d "S:\Fiscal Yr. 2015"
set _cd=%CD%
for /f "delims=" %%i in ('dir *.doc /a-d /s /b') do (
set old_fn=%%~fni
set new_fn=!old_fn:%_cd%\=!
set new_fn=!new_fn:Data Sheets\=!
set new_fn=!new_fn:\= !
echo ren "!old_fn!" "!new_fn!"
)
endlocal
The for command enumerates all the .doc files in in the current folder and sub-folders (\s switch). The \b switch makes it output the filenames using bare format (which in this case includes the full path). The /a-d makes it ignore directories, so only files get renamed.
Using setlocal / endlocal makes it possible to use delayed expansion of variables, and thus we use ! instead of % with the set commands. Delayed expansion ensures that the variables are reevaluated when they are used, instead of using the original value assigned to them.
The echo ren ... makes it echo the rename commands to the console instead of actually performing the renames. You'll want to verify it outputs the right rename statements before you run it for real, and it would be a good idea to backup the folder structure before you do it for real since there is no undo for this type of operation.
The batch file removes the leading path and only keeps the sub-folder's name, adding it before the filename, also removing the Data Sheets folder name from the new filename.

Script copies more then *.pdf

I have a script that copies several PDF Files, and it places it to the corresponding folder.
Script:
pushd "\\share\folder\"
for %%p in (*.pdf) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)
But it also copies files that are named like this: Test.pdf2098 or hello.pdf20j93f2
I just want it to copy *.pdf files and not PDF's that are invalid.
Add an if check to verify the extension
pushd "\\share\folder\"
for %%p in (*.pdf) do if /i ".pdf"=="%%~xp" for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)
David Ruhmann provided a workaround, but did not explain why your code fails.
The problem is that files on NTFS volumes that do not meet the old 8.3 short file naming standard are automatically assigned alternate short file names that do meet the standard. Files like xxx.pdf2098 would be given a short name that has a .pdf extension.
The windows commands like COPY, MOVE, FOR, etc. that search file names all attempt to match both the long and the short name, thus leading to your problem.
It is possible (and often recommended) to disable short file name generation on NTSF volumes, but any existing short names will remain and potentially still cause problems.
So David Ruhmann is correct in suggesting that you verify the file extension of each file.
Another frequently used method to verify the extension is to pipe DIR /B to FINDSTR:
for /f "eol=: delims=" %%p in (
'dir /a-d /b *.pdf^|findstr /lie ".pdf"'
) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "\\share2\folder\%%~n\%%~nxp"
)

Set file name with * as variable batch script

I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd

DOS Batch file - Copy file based on filename elements

I need to sort alot of files based on their filename. I would like to use a batch file to do it. I do know what I want but I am not sure of the correct syntax.
Example of filenames I work with: (They are all in the same directory originally)
2012_W34_Sales_Store001.pdf
2012_W34_Sales_Store002.pdf
2012_W34_Sales_Store003.pdf
2012_November_Sales_Store001.pdf
2012_November_Sales_Store002.pdf
2012_November_Sales_Store003.pdf
I would like to extract the information that are located between the "_" signs and put them into a different variable each time. The lenght of the informations contained between the _ signs will be different everytime.
Example:
var1="2012"
var2="W34" (or November)
var3="Sales"
var4="001"
If I am able to do this, I could then copy the files to the appropriate directory using
move %var1%_%var2%_%var3%_%var4%.pdf z:\%var3%\%var4%\%var1%\%var2%
It would need to loop because I have Store001 to Store050.
Also, there are not only Sales report, many others are available.
I hope I am clear.
Please help me realize this batchfile!
This script will make sure that it only attempts to move files that meet the pattern part1_part2_part3_part4.pdf
#echo off
for /f "eol=_ delims=" %%F in (
'dir /b *^|findstr /ix "[^_]*_[^_]*_[^_]*_[^_]*[.]pdf'
) do for /f "eol=_ tokens=1-4 delims=_." %%A in ("%%F") do (
move "%%F" "z:\%%C\%%D\%%A\%%B"
)
If needed, you could add md "z:\%%C\%%D\%%A\%%B" 2>nul before the MOVE in case the folders might not exist yet.
This script will move your files based on the values between the underscore to a like wise constructed path.
for %%f in (*.pdf) do call :handlefile %%f
:handlefile
set pad=z:
for /f "delims= tokens=1,* " %%a in ("%1") do call :step %%a %%b
rem this MOVES the file, maybe use echo first for testing
move "%fn%" "%pad%"
exit /B
:step
if !%2!==!! EXIT /B
set pad=%pad%\%1
for /f "delims=_ tokens=1,* " %%a in ("%2") do call :step %%a %%b
EXIT /B