Batch programming / rename files in a loop

Tags:
Batch

Hello, I have many files in a folder: Example "SecRp201508_XXXXXXX". This have to be changed to a new month value like "SecRp201509_XXXXXXX". Over a batch program, I can manage to change only the last file in the folder:

Attempt 1:

set nm=1509_

for /r %%i in ("*xls") do set Base=%%~ni

set Eins=%Base:~0,7%

set Zwei=%Base:~12,19%

set Neu=%eins%%nm%%zwei%

ren %Base%.xls %Neu%.xls
Result Attempt1:
ren Secrp201501_ZZZZZZ.xls Secrp201509_ZZZZZZ.xls 
(But only the last entry in the loop has been changed)
 
 

When I try it with this code the value %Base will not be transcribed. (For example the value "Eins" is "~0,7")

Attempt 2:

set nm=1509_

for /r %%i in ("*xls") do (set Base=%%~ni

set Eins=%Base:~0,7%

set Zwei=%Base:~12,19%

set Neu=%eins%%nm%%zwei%

ren %Base%.xls %Neu%.xls

)
 
result Attempt 2 :ren .xls .xls
 
How can I manage to change all files in the folder? Greetings Paul
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 7  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • pdraebel
    When dealing with IFS folders you should take a look at the QSHELL environment to handle such renames.
    7,545 pointsBadges:
    report
  • ToddN2000
    How often are you renaming the files, and for what purpose? It looks like they might relate to a date. If that is the case, how is historical data kept?
    126,100 pointsBadges:
    report
  • TheRealRaven
    Can you identify what environment your script runs in? And depending on environment, what shell are you using? It doesn't look correct for Qshell, so I'd guess it's one of the PASE shells or one you've installed yourself. Also, what is your OS version?

    It might also be necessary to show a little more of your script. The first example isn't clear how you want thr DO-group ended.

    If you replace your "ren" command with `echo` or `print` to see each name, what is the result? (And what is "ren"? Is that a home-grown utility? I'm not aware of it as a command in any of the standard shells.)
    32,470 pointsBadges:
    report
  • ProgrammerAA

    Hello,

    This is not a program on IBM i.

    Its an simple Batch Program on Windows.

    The files where the rename is necessary are monthly reporting files therefor we need the change from "SecRp201508_XXXXXXX" to SecRp201509_XXXXXXX.

    Greetings

    Paul

    35 pointsBadges:
    report
  • TheRealRaven
    Ah! So it's a Windows question, not "AS/400" at all. Sorry, it was confusing, especially since it closely matched various shell scripting languages and was tagged "AS/400". Makes more sense this way.

    When we reach 2016, do the file names need to change again while also reflecting the new year in addition to the month?
    32,470 pointsBadges:
    report
  • ProgrammerAA

    Yes, in 2016 the File name will change from "SecRp201512_XXXXXXX" to "SecRp201601_XXXXXXX" 

    Thank you for your support.

     

    35 pointsBadges:
    report
  • ProgrammerAA
    Are there furher comments to this problem?
    35 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: