Jump to content

for in CMD scripts


Recommended Posts


The pause is good. I will use the script more times, so to be sure the path is OK.

Note: The script sometimes leave extra _ in begin of filename and sometimes it removes one more letter (that should not be deleted) from begin of string. The second problem is not often, so not much problem. I don't want to bother you with it. But I have one more question I need to ask, in different thread.

Edited by DosCode
Link to comment
Share on other sites

Right now this happened to me with these files:

gen_0_3.pdf

gen_2_1.pdf

gen_4_1.pdf

gen_4_2.pdf

so the


FOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "gen_*.pdf"') DO CALL :RN "%%#" "%%~nx#"
GOTO :BREAK
:RN
SET "_FN=%~2"
::ECHO=
REN %1 "%_FN:~7%"
:BREAK

causes they are renamed to

.pdf

.pdf

.pdf

.pdf

Edited by DosCode
Link to comment
Share on other sites

Those files don't follow your original specification!

This may suffice for that scenario:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "gen_*.pdf"') DO CALL :RN "%%#" "%%~nx#"
GOTO :EOF
:RN
SET "_FN=%~2"
REN %1 "%_FN:~4%"

Link to comment
Share on other sites

With all due respect, DosCode, what you're interested in are "CMD Scripts", not "DOS Batch Files". Your insistence in using "DOS batch" in the titles of your threads is misleading, so please do use "CMD Script" not "DOS Batch" for any new topic you start in the future. Thanks.

Link to comment
Share on other sites

Does the number "4" at last line mean how the search string is long? I mean gen_ has 4 letters

I believe that you have already been referred to the website "ss64.com", but you might want to read this page - http://ss64.com/nt/syntax-substring.html - which explains in detail how to "extract part of a variable (substring)".

Cheers and Regards

Link to comment
Share on other sites

Does the number "4" at last line mean how the search string is long? I mean gen_ has 4 letters

From:

SET /?

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...