1

I have a script which prepends zeros in the file name if the file name is less that 18 digits.

For example if the file name is 1234567, then it should rename it to 000000000001234567 and if it is 123456789, then it makes it 000000000123456789. My code is already doing this functionality

Now my requirement is that if the file name is 1234567-1, then it should make it 000000000001234567_Type_1 and if name is 1234567-a, then it should rename it to 000000000001234567_Type_a.

This means it should keep doing what my script doing but in addition it should suffix _Type_<last character> in the file name. File name can be 1234567 or 123456789 or 1234567-1 or 12345678-b or any thing else but there are always digits and not characters before hyphen (-). There can be multiple files in a folder. Hyphens (-) should be replaced with underscores (_) followed by Type_ and whatever is the last character in the file name.

Here is my existing code:

@echo off
setlocal enableextensions enabledelayedexpansion

rem iterate over tif files:
 for %%f in (C:\Task\Drop_Files\*.tif) do (
    rem store file name without extension
    set FileName=%%~nf

    rem Add leading zeroes:
    set FileName=00000000000000000!FileName!

    set FileName=!FileName:~-18!

    set FileName=!FileName!%%~xf

    rem Rename the file
    rename "%%f" "!FileName!"

)

4 Answers 4

1

The following script should handle files with the hyphen suffix (like 1234567-1) and without (like 1234567) as you want:

@echo off
setlocal enableextensions enabledelayedexpansion

rem Iterate over TIF files:
for %%f in ("%~dp0*.tif") do (
    rem Store file name without extension:
    set "FileName=%%~nf"

    rem Split off hyphen and suffix:
    set "Suffix=!FileName:*-=!"
    if "!Suffix!"=="!FileName!" set "Suffix="
    call set "FileName=%%FileName:-!Suffix!=%%"
    if defined Suffix set "Suffix=_Type_!Suffix!"

    rem Add leading zeroes:
    set "FileName=00000000000000000!FileName!"

    rem Build final file name:
    set "FileName=!FileName:~-18!!Suffix!"

    rem Rename the file:
    rename "%%~ff" "!FileName!%%~xf"
)

What I did:

  • introduced a new variable Suffix that will hold the character after the hyphen (for instance 1 when the file name is 1234567-1); the suffix including the hyphen (like -1) will be removed from the file name (this would fail in case -1 occurred more than once in the file name though);
  • improved set syntax by adding quotes around the assignment expression;
Sign up to request clarification or add additional context in comments.

6 Comments

If the original file name is 0000000000012783_1 then Your code is producing the file name as 0000000000012783_1_Type_0000000012783_1. It should be 0000000000012783_Type_1. Can you please advise what to do?
Sorry, I failed to apply delayed expansion for Suffix once -- see my edit...
Sorry, I had not tested the code extensively last week. It is working for some scenarios but not for one in particular. when the file name does not have any hyphen(-), then it does not work. For example, file name is 000075431. The script is converting it into 000000000000075431_Type_000075431. I want it to be 000000000000075431. Since there is no hyphen followed by any character so it should just make it 18 digits long. Can you please help?
Oops, I missed that when reading your question obviously; see my edit which fixes that...
is not doing anything now. Nothing happens
|
1

Simple. In your original code, just before the "Rename" line, just add the following line:

set FileName=!FileName:-=_Type_!

That's it!

3 Comments

This would lead to a numeric part with 16 digits only for files suffixed with the hyphen part...
@aschipfl Thanks for your feedback but can you give me an example filename for which my solution wouldn't work?
For example: 1234567-1; the result would be 0000000001234567_Type_1 (16 digits only before _Type_ although there should be 18)...
0

I think the following, slightly modified code, should do what you're looking for. Basically, you need to test whether the next to last character is a dash (-), in which case you replace it with _Type_. To do that, you extract the last 2 characters of the FileName (without extension) then look at the first character of those two extracted chars. If it's a dash, then you eliminate the last two characters from the original filename because they will eventually be replaced with the new _Type_[char] construct.

@echo off
setlocal enableextensions enabledelayedexpansion

rem iterate over tif files:
 for %%f in (C:\tests\output*.txt) do (
    rem store file name without extension
    set FileName=%%~nf
    set IsDash=!FileName:~-2!
    set IsDash=!IsDash:~0,1!
    IF "!IsDash!" == "-" (
      SET FileType=_Type_!FileName:~-1!
      SET FileName=!FileName:~0,-2!
    ) ELSE (
       SET FileType=
    )

    rem Add leading zeroes:
    set FileName=00000000000000000!FileName!

    set FileName=!FileName:~-18!!FileType!

    set FileName=!FileName!%%~xf
    rem Rename the file
    rename "%%f" "!FileName!"
)

Comments

0
@echo off
setlocal enableextensions enabledelayedexpansion

rem Change current directory
cd /D C:\Task\Drop_Files

rem Iterate over tif files:
for /F "tokens=1-3 delims=-." %%f in ('dir /B *.tif') do (

    rem Store file name without extension and leading zeroes:
    set "FileName=00000000000000000%%f"

    rem Get the right type and input name:
    rem - If input name is "1234567.tif" then "%%h" is empty, so there is no Type part
    rem - If input name is "1234567-1.tif" then "%%h" is "tif", so the Type is "_Type_%%g"
    if "%%h" equ "" (
        set "input=%%f.%%g"
        set "type="
    ) else (
        set "input=%%f-%%g.%%h"
        set "type=_Type_%%g"
    )

    rem Rename the file
    rename "!input!" "!FileName:~-18!!type!.tif"

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.