Mike
Mike

Reputation: 133

Appending Variable String to File Names

I'm trying to rename 5 specific files with an appended string. They all follow the same name format but have different lengths.

I would like to append the text "Apr-17_RA" in this folder

In this example the batch would be run May 1st, so get dates would be -1 to run previous month.

String1=Get-Date -format MMM
String2=Get-Date -format YY
String3=_RA

File location: \\[SERVER]\TopFolder\F1\F2\F3\[X_FILE-NAME_].csv

The file should say \\[SERVER]\TopFolder\F1\F2\F3\[X_FILE-NAME_]& St1 & Str2 & Str3.csv

Final Name for 1 File: \\[SERVER]\TopFolder\F1\F2\F3\[X_FILE-NAME_Apr-17_RA.csv

EDIT for current batch:

:assign variable for your file prefix
set filesuffix=Apr-17_RA

:create a temporary drive mapping to the target folder
net use m: "\\[Server]\[F1]\[F2]\[F3]\[F4]"

:change working directory to target folder
cd /d m:

:rename target files
ren [X_File-1_].csv [X_File-1_]%filesuffix%.csv
ren [X_File-2_].csv [X_File-2_]%filesuffix%.csv
ren [X_File-3_].csv [X_File-3_]%filesuffix%.csv
ren [X_File-4_].csv [X_File-4_]%filesuffix%.csv
ren [X_File-5_].csv [X_File-5_]%filesuffix%.csv

:remove the temporary mapping you created earlier
net use m: /delete

Upvotes: 0

Views: 68

Answers (1)

Compo
Compo

Reputation: 38664

Here is a remarked batch file which uses powershell. It will do the date work you haven't done, you can add your own specific commands in the appropriate location to finish the script yourself:

@Echo Off
Rem Enter date format string
Set "String1=MMM-yy"
Rem Enter date string suffix
Set "String2=_RA"
Rem Enter relative days from today, [use - for days ago].
Set "RelDays=-1"

For /F UseBackQ %%A In (
    `PowerShell "(Get-Date).AddDays(%RelDays%).ToString('%String1%')"`
) Do Set "RDate=%%A%String2%"

Rem Enter commands using new string variable
Echo(%%RDate%% = %RDate%

Timeout -1
GoTo :EOF

Upvotes: 1

Related Questions