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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Using parameters to rename a file

Reply to Message Icon

Name: R475
Date: January 19, 2007 at 10:03:16 Pacific
OS: 2k
CPU/Ram: .
Product: .
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




Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: January 19, 2007 at 12:09:26 Pacific
Reply:

@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



0

Response Number 2
Name: R475
Date: January 19, 2007 at 13:54:16 Pacific
Reply:

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.



0

Response Number 3
Name: IVO
Date: January 19, 2007 at 14:21:37 Pacific
Reply:

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


0

Response Number 4
Name: R475
Date: January 19, 2007 at 14:54:41 Pacific
Reply:

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.


0

Response Number 5
Name: IVO
Date: January 20, 2007 at 04:00:20 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: R475
Date: January 22, 2007 at 09:49:30 Pacific
Reply:

Thanks Ivo!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Program to check mouse an... PHP E-mail with Styleshee...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Using parameters to rename a file

Batch file to rename a file www.computing.net/answers/programming/batch-file-to-rename-a-file/14356.html

Help needed renaming a file in DOS www.computing.net/answers/programming/help-needed-renaming-a-file-in-dos/12770.html

Rename a file with time www.computing.net/answers/programming/rename-a-file-with-time/11595.html