Computing.Net > Forums > Programming > Using parameters to rename a file

Using parameters to rename a file

Reply to Message Icon

Original Message
Name: R475
Date: January 19, 2007 at 10:03:16 Pacific
Subject: Using parameters to rename a file
OS: 2k
CPU/Ram: .
Model/Manufacturer: .
Comment:

I want to use a command line parameter to fill in the "FILENAME_HERE" section of the following batch file code (see below). I tried using %3 , but it didn't work. Thanks in advance.

@Echo Off

For %%A in (%~f1\xx1cxt00xx*.txt) Do Call :REN %~f1 %%A

GoTo :EOF

:REN
Set File=%~nx2
Ren %~f1\%File% FILENAME_HERE%File:~10%
GoTo :EOF



Report Offensive Message For Removal

Response Number 1
Name: IVO
Date: January 19, 2007 at 12:09:26 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

@Echo Off
Set FileName=%2
For %%A in (%~f1\xx1cxt00xx*.txt) Do Call :REN %~f1 %%A

GoTo :EOF

:REN
Set File=%~nx2
Ren %~f1\%File% %FileName%%File:~10%
GoTo :EOF

Where MyBatch File FileName



Report Offensive Follow Up For Removal

Response Number 2
Name: R475
Date: January 19, 2007 at 13:54:16 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

Thanks Ivo. That works perfectly.

Right now the source directory is passed in as an arguement, is there a way to "hard code" the directory into this program? By the way, the source directory I'm hoping to use is going to have spaces.



Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: January 19, 2007 at 14:21:37 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

@Echo Off
Set Path="My Path with Blanks"
Set FileName=%1
For %%A in (%Path%\xx1cxt00xx*.txt) Do Call :REN %%A
GoTo :EOF

:REN
Set File=%~nx1
Ren %Path%\%File% %FileName%%File:~10%
GoTo :EOF

Where now MyBatch FileName and PathName hardcoded in the script and *embraced* by ".


Report Offensive Follow Up For Removal

Response Number 4
Name: R475
Date: January 19, 2007 at 14:54:41 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

Thanks again!

Just out of curiosity is there a way to set the path (one with spaces) as a command line parameter? Using the old code.


Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: January 20, 2007 at 04:00:20 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

First of all, please replace the Path variable name with e.g. Folder as Path is a system reserved identifier and using it to name a variable may lead to unpredictable side effects. That was my mistake.

Then to get either Folder with spaces and Filename from the command line the easiest way is to change their position on the command tail, i.e.

BatchName FileName "My Path with Spaces"

where Fikename has no blanks and the Path/Folder is embraced by ".

The following script achieves the target

@Echo Off
For /F "tokens=1,*" %%A in ("%*") Do (
Set FolderName=%%B
Set FileName=%%A)

For %%A in (%FolderName%\xx1cxt00xx*.txt) Do Call :REN %%A
GoTo :EOF

:REN
Set File=%~nx1
Ren %FolderName%\%File% %FileName%%File:~10%
GoTo :EOF


Report Offensive Follow Up For Removal


Response Number 6
Name: R475
Date: January 22, 2007 at 09:49:30 Pacific
Subject: Using parameters to rename a file
Reply: (edit)

Thanks Ivo!


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Using parameters to rename a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software