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"
)
Related
I have PDF files in one location (all in the same folder), which I need to take 3 useful information from the file name.
And I have .jpg files in another location (1 picture per folder) which I need to rename with these information taken from the PDF.
My script is able to find the information, store and rename but it only works for the first file in a directory and then stops.
I need to make it run in a loop until there is either no more PDF files to take information from OR no more .jpg files to be renamed.
Can someone help me to make this script run in a loop?
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%\
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
)
rem Rename the pictures using the values stored on the variables
xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node %Node%\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node%Node%_RL%RcvLn%_GS%GunStn%.jpg"
You set each variable inside of the loop for each file, but then you do the xcopy outside of the loop which will only do the xcopy once. So we rather do the xcopy inside the loop.
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
echo xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node !Node!\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node!Node!_RL!RcvLn!_GS!GunStn!.jpg"
)
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.
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.
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.
I have read numerous articles now and it's not clear and there's lots of versions and this that and the other and I have been piecing things together and have got so far, my problem is the 'rar' command doesn't seem to accept my substition variable and instead reads it as a string.
But this is what I have
#echo off
SETLOCAL
set path=%path%;"C:\TEMP\Output"
set _sourcedir=C:\TEMP\Output
set _logfile=c:\temp\Output\zip_log.txt
set _rarpath=C:\Program Files (x86)\WinRAR
echo Starting rar batch > %_logfile%
:: Set default directory
pushd %_sourcedir%
echo Scan Directory is %_sourcedir%
FOR %%f IN (*.txt) DO (
echo %%f
%_rarpath\rar.exe a test
)
popd
ENDLOCAL
#echo on
I have cut some out and chopped it so you only get the essence, I haven't omitted any commands though.
I am trying to loop through the directory and locate all .txt files and zip them into a .rar file.
The echo writes out the correct filenames.
Any ideas?
I think this is your problem:
set _rarpath=C:\Program Files (x86)\WinRAR
In batch files, the environment variable delimiter is a space, so it thinks _rarpath is C:\Program
Enclose the path in double quotes and see if that helps:
set _rarpath="C:\Program Files (x86)\WinRAR"
Also, in your FOR loop change
%_rarpath\rar.exe a test
to
%_rarpath%\rar.exe a test
(or,perhaps this was a typo?)
I don't see where you're asking winrar to do anything with your files? %%f needs to be on the winrar command line somewhere.
Also, you shouldn't need a loop at all for this: rar.exe a test.rar %yourpath%*.csv or similar.