Link to home
Start Free TrialLog in
Avatar of 9thTee
9thTee

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

Avatar of NVIT
NVIT
Flag of United States of America image

> 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.

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\110121125704, so I've used a value of C:\folder\FolderAbovePDF

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.

@echo off
setlocal enableDelayedExpansion

set SrcDir=C:\folder\FolderAbovePDF
set TgtDir=C:\Tgt

if not exist "%SrcDir%\*." goto :NothingNew

echo.
pushd "!SrcDir!"
for /d %%I in (%SrcDir%\*) do (
  set Ifn=%%~nI
  for /r %%a in (*) do (
    set fn=%%~na
    set fe=%%~xa
    set FNNew=!Ifn!-!fn!!fe!
    set ExsDir=%%~dpa
    ECHO ren "%%a" "!FNNew!"
  )
  ECHO move "!ExsDir!*.pdf" "%TgtDir%" && rd /y "!ExsDir!"
)
popd
goto :Done

:NothingNew
echo No dir found. Nothing to process

:Done

Open in new window

Avatar of oBdA
oBdA

Here's a PowerShell version. It's in test mode and will not actually move anything; remove the -WhatIf at the end of line 5 to run it for real.
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}
}

Open in new window

A slightly different cmd approach, also doing nothing because of test mode:
@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

Open in new window

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.  
Avatar of 9thTee

ASKER

Hi NVIT,
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

Open in new window


The file name keeps growing with each directory.  Kinda like the directory name needs to be reset after each loop.

Any ideas?

Thanks,
Mark


Avatar of 9thTee

ASKER

Hi Qlemo,
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
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, forget a wildcard:
@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

Open in new window