1.

Solve : Copying, moving and renaming of files?

Answer» Scenario:
I have a folder called box that contains a number of folders with names A, B, C… These folders contain varying number of files with a serialized naming scheme Zxx, where, x represents a number (0, 1, 2…).

Action:
I would like to have a batch file that performs the following:
  • CREATE a destination folder whose name is given to a placeholder at the same place as box.
  • Accesses the folders in box alphabetically
  • Copies their files into the destination folder with a serialized naming scheme of type Zxx with a possibility of getting to Zxxx.


This is a little difficult to start on because I am not quite certain what it is you are trying to do. Can you show an example of what you are looking for?

From what I am reading, I think what you are trying to do is this:

TRANSFER File named "Z00" in folder "A" under folder "box"
...
to
...
File named "Z000" in folder "Destination"

Please let us know if this is indeed what you are trying to do or if it is something different.There is another similar topic to this one in which Salmon Trout and CN-DOS replied with some code that does almost what you are trying to do here.

Quote from: Salmon Trout on August 24, 2011, 01:56:50 PM
Your convention of having 2 dots was what I did not know about... try this. For guidance there are many sites and one of the best is ss64.com

Code: [Select]@echo off
setlocal enabledelayedexpansion

For /f "delims=" %%F in ('dir /s /b *.pdf') do (
set OldPathAndName=%%~dpnxF
Set OldName=%%~nxF

Set NewName=!OldName!

set NewName=!NewName:And=and!
set NewName=!NewName:The=the!
set NewName=!NewName:Is=is!
set NewName=!NewName:Not=not!

echo Ren "!OldPathAndName!" "!Newname!"
)

Quote from: CN-DOS on August 31, 2011, 05:53:17 AM
Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /s /b *.pdf') do (
set NewName=%%~na
set NewName=!NewName:And=and!
set NewName=!NewName:The=the!
set NewName=!NewName:Is=is!
set NewName=!NewName:Not=not!
ren "%%a" "!Newname!.pdf"
)

These pieces of code are meant to rename the files and change the names to have lowercase "and"s, "the"s, "is"s, and "not"s, but uses the same principles (and commands) that you would WANT to use for a program like this. The code that I am writing is going to copy the files to the destination folder and rename them with a Zxxx format, but the first x will be the folder they were originally from. So Z00 in folder A will become ZA00 in the new location.

The coding that I am using uses some stuff that is going to require some work on your part. I am going to assume for this code that the "box" folder you are referencing is located on the desktop of "User" and the "destination" folder is in the same location. This is important because you will notice the :~ that is used in the code to parse out the file name. You are going to have to do the research to find out exactly how many characters there are in the fully qualified path name.

Here is a short explanation of what you'll see. To parse out a variable, you use the ':~' to designate that you only want part of the variable. The first number will be the number of characters there are before the start of the parsing, and the second number is the number of characters to use in the parsing.
Example:
%date% will expand to Wed 09/14/2011
%date:~4,2% will expand to 09
Utilizing one number "N" will utilize the remainder of the variable after 'n' characters
%date:~10% will expand to 2011

With that, here is a rough go at what you are trying to do with the limited knowledge I have on the FOR command:

Code: [Select]@echo off
setlocal enabledelayedexpansion

set NewFilePath=C:\Users\User\desktop\destination {your full path of destination folder}

for /f "delims=" %%F in ('dir /s /b /o:n') do (
set OldFilePath=%%dpF
set OldName=%%~nxF
set OldFilePathAndName=%%dpnxF
set Folder=!OldFilePath:~26,1! {Here's where you need to do your research}

set NewName=!OldName:~0,1!!Folder!!OldName:~1!

copy !OldFilePathAndName! !NewFilePath!

ren !NewFilePath!\!OldName! !NewName!
)
Side note on the :~

You can utilize the - to utilize a from the end type of selection. So a !OldFilePath:~-2,1! Will take the second to last character in the path name (the last character always being a \). Or if you know you want to utilize only a certain part of the path that starts 12 characters in and you don't want the last slash in there, you can set up !OldFilePath:~11,-1! Or use it however you need to. I missed a couple of ~s in the original code.

Corrected.

Code: [Select]@echo off
setlocal enabledelayedexpansion

set NewFilePath=C:\Users\User\desktop\destination {your full path of destination folder}

for /f "delims=" %%F in ('dir /s /b /o:n') do (
set OldFilePath=%%~dpF
set OldName=%%~nxF
set OldFilePathAndName=%%~dpnxF
set Folder=!OldFilePath:~26,1! {Here's where you need to do your research}

set NewName=!OldName:~0,1!!Folder!!OldName:~1!

copy !OldFilePathAndName! !NewFilePath!

ren !NewFilePath!\!OldName! !NewName!
)
This is what I mean:

Transfer file named "z00" in folder "A" under folder "box"
...
to
...
File named "z00" in folder "Destination"

Continue the above procedure till the last file in “A” is transferred and renamed into “Destination”

Transfer file named "z00" in folder "B" under folder "box"
...
to
...
File named "zxx" in folder "Destination”, where "xx" is a serial increment of the last file named in folder "Destination". Example, if the last file transferred to "Destination" was named "z56", then, the first file transferred to "Destination" from "B" is named "z57".
This continues until the last file in the last folder of “box” has been transferred to "Destination".
Note:
The folders in “box” are accessed in alphabetical order.
Only the last two characters of the file names change and this may change to three characters.
The folder “Destination” is named by the user via place holder and is located on the same path as “box”.

I am going through the script you posted will post the outcome later. Thanks for ss64.com it helps.
This becomes a two step process then. The first step moves the file into a temporary location so as not to rename the wrong file or copy the wrong file at any point. The second step is to ensure that the proper naming convention is used. A simple if command will ALLOW for the naming convention to be correct. Please say something if there are parts of the script that you don't understand.

Code: [Select]@echo off
setlocal enabledelayedexpansion

:setnewpath
set /p NewFilePath=Please enter the full path of the destination folder
cls
if exist %NewFilePath% goto continue
echo That location is not valid. Please type a valid location for the destination.
echo The path needs to exist currently, so please make the folder if it does not already exist.
goto setnewpath

:continue
set TempFilePath="c:\users\user\desktop\Temp"
set number=0

for /f "delims=" %%F in ('dir /s /b /o:n') do (
set OldName=%%~nxF
set Extension=%%~xF
set OldFilePathAndName=%%~dpnxF
if /i !number! leq 9 (
set NewName=z00!number!!Extension!
) else (
if /i !number! leq 99 (
set NewName=z0!number!!Extension!
) else (
set NewName=z!number!!Extension!
)
)

copy !OldFilePathAndName! !TempFilePath!

ren !TempFilePath!\!OldName! !NewName!

copy !TempFilePath!\!NewName! !NewFilePath!
del !TempFilePath!\!NewName! /q

set /a number=!number!+1
)
rd c:\users\user\desktop\Temp /q
Over thinking computer tasks always leads to complexities. Try to keep it simple.

Code: [Select]@echo off
setlocal enabledelayedexpansion

set seq=0
set source=c:\temp\box
set /p target=Enter Destination Folder:

md %target%

for /f "tokens=* delims=" %%i in ('dir %source% /a:-d /s /b') do (
set leadzero=000!seq!
set leadzero=!leadzero:~-3!
copy %%i %target%\Z!leadzero!%%~xi
set /a seq+=1
)

You will be prompted for a fully qualified destination directory. The source directory (box) is hardcoded but feel free to change it.

Good luck.



Discussion

No Comment Found