Windows 10: batch script to Add characters to filename if doesnt exist

Discus and support batch script to Add characters to filename if doesnt exist in Windows 10 Gaming to solve the problem; I'm trying create a batch file to add characters to filenames if it doesnt already exist. for example, "test.txt" would change to "test.tmp.txt" and... Discussion in 'Windows 10 Gaming' started by Jake Masters1, May 22, 2025 at 5:07 PM.

  1. batch script to Add characters to filename if doesnt exist


    I'm trying create a batch file to add characters to filenames if it doesnt already exist. for example, "test.txt" would change to "test.tmp.txt" and "AlsoTest.tmp.txt" wouldn't change at all because it already contains the ".tmp" string in the filename.Thank you.

    :)
     
    Jake Masters1, May 22, 2025 at 5:07 PM
    #1

  2. How to append another character if filename results in duplicate

    Hi, my name is Kelly, and I'm an independent consultant and I'm here to help.

    To append a character or two to the end of the filename if it may result in a duplicate filename, you can modify your Batch script to check if the new filename already exists in the directory and, if it does, append a number or letter to the end of the filename. Here's an updated version of your script that does this:

    @Echo off

    setlocal enabledelayedexpansion

    for /f "tokens=3 delims=" %%a in ('dir /b *.pdf') do (

    set "newname=%%a.pdf"

    if exist "!newname!" (

    set "counter=1"

    set "basename=%%a"

    :loop

    set "newname=!basename!!counter!.pdf"

    if exist "!newname!" (

    set /a "counter+=1"

    goto :loop

    )

    )

    ren "%%a" "!newname!"

    )

    This script uses a counter variable to keep track of the number to append to the filename. If the new filename already exists in the directory, the script appends a number to the end of the filename and checks if the resulting filename exists. If the resulting filename also exists, it increments the counter and tries again until it finds a unique filename.

    I hope this helps! Let me know if you have any further questions or concerns.
     
  3. bg3075 Win User
    How to append another character if filename results in duplicate

    Hello folks. So, I've created a Batch script that will rename files in the same directory, renaming them with text between the second and third "_" character in the original filenames. But, of course, it skips resulting duplicates. Is there possibly a way to have it append a character or two to the end of the filename if it may result in a duplicate filename, such as (1) or _a?

    Script:

    @Echo off

    for /f "tokens=3 delims=_" %%a in ('dir /b *.pdf') do (

    ren "*_%%a_*" "%%a.pdf"

    )
     
  4. batch script to Add characters to filename if doesnt exist

    Which non-alphabetic and non numeric characters are accepted in a filename?

    Thank you Mr_Meow and Rodrigo.Queiroz, but playing a bit with the directory list I was given, I realize there is one step I cannot resolve.

    I can find from the lists I was given, the names that have illegal characters but it is a bit complicated to do:

    "filename","full path/filename","owner","creation date","loast modified date"

    As soon as I try a Text-to-column action in Excel, the lines having illegal character may mess up the column twice and working from there to find illegal filenames is more complicated. I could simply load up the CSV and not do a text-to-column and then find the illegal filenames, it would be easier. But the problem is the next step.

    Even if I isolate the illegal filenames, the only known way to fix this is to give the owners those filenames and ask them to rename them. This will take a few months to do because it will be their lowest priority and they are pretty busy right now.

    Isn't there a tool existing that can scan a server or directory to find all invalid filenames and somehow offer a solution to rename them (depending on the case)?

    If not, I don't see how this will be fixed with in a few days (no deadline, its just that this has been going on for so long) ...
     
Thema:

batch script to Add characters to filename if doesnt exist

Loading...
  1. batch script to Add characters to filename if doesnt exist - Similar Threads - batch script Add

  2. batch script to Add characters to filename if doesnt exist

    in Windows 10 Software and Apps
    batch script to Add characters to filename if doesnt exist: I'm trying create a batch file to add characters to filenames if it doesnt already exist. for example, "test.txt" would change to "test.tmp.txt" and "AlsoTest.tmp.txt" wouldn't change at all because it already contains the ".tmp" string in the filename.Thank you....
  3. Batch scripting

    in Windows 10 Software and Apps
    Batch scripting: Hi, here's the code that I used for 3 DISM commands to start automatically after pressing a key:Code and result:Code:@echo off echo You are going to use DISM. echo 1. To check the image. echo 2. Scan your PC's health echo 3. Restore your PC'S health. echo To continue, press...
  4. Batch scripting

    in Windows 10 Gaming
    Batch scripting: Hi, here's the code that I used for 3 DISM commands to start automatically after pressing a key:Code and result:Code:@echo off echo You are going to use DISM. echo 1. To check the image. echo 2. Scan your PC's health echo 3. Restore your PC'S health. echo To continue, press...
  5. Batch scripting

    in Windows 10 Network and Sharing
    Batch scripting: Hi, here's the code that I used for 3 DISM commands to start automatically after pressing a key:@echo off echo You are going to use DISM. echo 1. To check the image. echo 2. Scan your PC's health echo 3. Restore your PC'S health. echo To continue, press any key. echo Before...
  6. PageFile.sys batch script

    in Windows 10 Virtualization
    PageFile.sys batch script: Hello, I need help writing a batch script that would configure Windows Virtual Memory based on the amount of RAM installed If you have 4 GB of RAM, set virtual memory to 8 GB (Custom size: set both initial and maximum size to 8192) If you have 8 GB of RAM, set virtual memory...
  7. Batch Scripts / Programs.

    in Windows 10 Performance & Maintenance
    Batch Scripts / Programs.: The members and quests who use this forum know how professional, informative and helpful it is! In order to help members solve their problems/issues we often ask them to run certain programs and perform certain tasks and then upload [ post ] the results to the forum for us...
  8. Batch scripting problems

    in Windows 10 Ask Insider
    Batch scripting problems: So i found a bug. In batch scripting, I can only store 3 variables. I don't know the cause for this. Here's the code, if you need it. @ echo off title Elevator OS by Lackmey on YouTube and Reddit set gdata=No data found (this one works) set 1data=No data found (this one...
  9. Removing characters from filenames

    in Windows 10 Network and Sharing
    Removing characters from filenames: Hello Apologises if this question is repeated somewhere, How can I remove a series of numbers and words in bulk from a folder of word documents please? Each filename contains different numbers and words but they follow the same format of: (9 numbers)-(5...
  10. Batch script question

    in Windows 10 Support
    Batch script question: Hello, I want make a batch script to delete a spefic folder on the C disk, but how can I get only that spefic folder? I got this: Code: FOR /D %%p IN ("C:\USERS\%USERNAME%\Downloads\*.*") DO rmdir "%%p" /s /q cd C:\USERS\%USERNAME%\Downloads\ del *.* /q /s timeout /t 5...