asked on
Need DOS Script to move files in directories to the base directory and rename the files based on the directory name.
I have a Toshiba MFP that we scan documents sets as pdf's to a windows share. A scanned document set may include one document or perhaps over 100. Each document set goes in a single time stamped directory such as 110121125704 and the pages in each directory are named Page0001.pdf, Page0002.pdf, etc.
I need a DOS script that will rename all the individual pages with the time stamped directory name prepended to the file name and place the files in another folder and then delete the original folder. The filename would look similar to 110121125704-Page0001.pdf.
The script should first check to see if there are any directories which means there are newly scanned documents there.
I have tried a variety of things but really not hit on a good way to do this.
Thanks,
Mark
It will only process folders in source that consist of exactly 12 digits.
If it helps, the timestamp could be changed to yyyyMMdd as well, which would make it easier to sort the files by date.
$source = '\\Server\\Share\Source'
$target = '\\Server\\Share\Target'
Get-ChildItem -LiteralPath $source -Directory | Where-Object {$_.Name -match '^\d{12}$'} | ForEach-Object {
$parent = $_
Get-ChildItem -LiteralPath $_.FullName -Filter *.pdf | Move-Item -Destination {"$($target)\$($parent.Name)-$($_.Name)"} -Verbose -WhatIf
If (-not (Get-ChildItem -LiteralPath $_.FullName)) {Remove-Item -LiteralPath $_.FullName -Verbose}
}
@echo off
setlocal enableDelayedExpansion
set srcDir=C:\Source
set dstDir=C:\Destination
for /d %%D in (%srcDir%) do (
for %%F in (%%D\*.pdf) do echo move %%~fF "%dstDir%\%%~nD %%~nxF"
)
rmdir %srcdir%\* 2>nul
The last command will remove empty folders, so only does something if the combined rename and move has been applied to all files in the corresponding folder. So should the scan run at the same time as the script, no conflicts will occur. And the next run will move the remaining files.BTW, the rd command options of NVIT's code line 20 should be /s /q or none.
ASKER
When I try your script, it almost works. If there is one directory in the source, it renames the files with the directory name which is perfect. However, lets say there are two directories in the source. The file names will be named with both of the directory names. If there are three directories in the source, then all three directory names make up the file name.
For example:
Directories in the source file are:
110121125704
110121125302
110121125622
The file names contained in the first directory will be:
110121125704-Page0001.pdf
110121125704-Page0002.pdf
The file names contained in the second directory will be:
110121125302-110121125704-Page0001.pdf
110121125302-110121125704-Page0002.pdf
110121125302-110121125704-Page0003.pdf
The file names contained in the third directory will be:
110121125622-110121125302-110121125704-Page0001.pdf
110121125622-110121125302-110121125704 -Page0003.pdf
The file name keeps growing with each directory. Kinda like the directory name needs to be reset after each loop.
Any ideas?
Thanks,
Mark
ASKER
I removed the echo command on line 8 from your script but when I run it, nothing happens. I am testing this on my Win 10 notebook in the command window.
Any ideas?
Thanks,
Mark
@echo off
setlocal enableDelayedExpansion
set srcDir=C:\Source
set dstDir=C:\Destination
for /d %%D in (%srcDir%\*) do (
for %%F in (%%D\*.pdf) do echo move %%~fF "%dstDir%\%%~nD %%~nxF"
)
rmdir %srcdir%\* 2>nul
> and the pages in each directory are named Page0001.pdf, Page0002.pdf, etc.
> I need a DOS script that will rename all the individual pages with the time stamped
> directory name prepended to the file name and place the files in another folder and
> then delete the original folder. The filename would look similar to 110121125704-Page0001.pdf.
This works.
Save the code to a .bat or .cmd file.
Change the SrcDir and TgtDir variable values to your folder locations.
IMPORTANT: Note that SrcDir value is the parent folder above the dated folder 110121125704 which contains the pdfs, In my post example, the actual location housing the pdf files is at
C:\folder\FolderAbovePDF\1
Run the file in a CMD window.
It is currently in test mode so you can see what the result looks like. When you are happy with it and you'd like to run it for real, remove the ECHO fronting the ren and move commands.
Open in new window