I am looking for help with a batch file(s) that will allow me to move all files of a particular type (*.PDF's for example) to a new folder. HOWEVER I would like to have features not readily apparent to me. (I am not a programmer) included in the process (see questions at the end of my message). I have searced the web exhaustively for a solution and I have not discover an app that does what I need. So, I came here looking for developed solutions. What I have discovered is that I can use ROBOCOPY to get all of my files into a directory using:
@echo off
set source="R:\Users\Public\"
set destination="R:\Users\Public\All_PDF_Files\"
ROBOCOPY %source% %destination% *.pdf /S /R:1 /W:1 /NDL /XD OutputFolderfor /r %source in (*) do @copy "%destination" .
But what I can not determine how to do is flatten the directory afterwards and get all files in one folder. I have seen a post or two (one from a user Max who wrote a batch file to flatten the directory and this is almost what I need. So starting from his batch files.... Assuming I have a directory tree containing only directories and subdirectories holding PDF files...and a destination folder identified...Max presented the following...
From Max Below...
I recently had to tackle this problem, and many files that I wanted to move to from the hierarchy to a single folder had the same name as each other, and I wanted to still flatten the hierarchy without them to being over-written. What I did was write a script that moves the file, but renames it with the old hierarchy path in the name for example: source files:
C:\files\somefiles\file.txt
C:\files\otherfiles\file.txt
destination is C:\newdir\ files are created as
C:\newdir\somefiles-file.txt
C:\newdir\otherfiles-file.txt
here is the code, batch file 1 goes thru the files, batch file 2 renames and moves them (could also copy instead, if you want to preserve the source:
@echo off
for /r %%f in (*.*pr) do @renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"
renameandmovefilespart2.bat
@echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem @echo shortened: %newname%
rem @echo source path: %origPathOnly% newPath: %startingDir%
rem @echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"
Questions:
1.) rather than prepending the file name with the source folder how can I postpend the file name with source file location? Do I simply rewrite "%%~ff" "%%~xf" to "%%FF~" "%%XF~"
2.) I do not want the files moved I would like to simply overwrite the source file
Note: I could delete the source file if need be!
More Question?
3.) What does (*.*pr) do in Max's first batch file?
4.) What does Max mean by these (Line 9) rem make this the starting directory, that you dont want in the newly renamed files (line 10) "rem make this the starting directory, that you dont want in the newly renamed files" in batch 2 above? Is he just indicating a subdirectory in the source directory?
No comments:
Post a Comment