rename a file in a batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
helene
Posts: 9
Joined: 07 Jul 2014 02:29

rename a file in a batch script

#1 Post by helene » 07 Jul 2014 06:08

i want to write a script in windows to rename a file by deleting the first number.
by example : files name 012345.txt, 012346.txt... my application supporte only 5 characters for a name so by renaming it should be 12345.txt, 12346.txt.. and so on.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#2 Post by foxidrive » 07 Jul 2014 08:02

Test this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do (
   set "name=%%~nxa"
   ren "%%a" "!name:~1!"
)
pause

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#3 Post by helene » 07 Jul 2014 11:13

excuse me foxidrive my files are locate in a folder name code in the c drive how can i directorate your script to work with my folder????

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: rename a file in a batch script

#4 Post by Squashman » 07 Jul 2014 11:37

Put the batch file in the same directory as the TEXT files and run it.

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#5 Post by helene » 08 Jul 2014 02:41

i did it but it show me files not find in the command prompt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#6 Post by foxidrive » 08 Jul 2014 02:56

How many .TXT files are in the same folder as the batch file?

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#7 Post by helene » 08 Jul 2014 03:02

it is a coming files from a server so sometimes its come by 3 or 10 or 1 and me i have to rename it and send it to another application which support only 6 characters as a name.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#8 Post by foxidrive » 08 Jul 2014 03:05

This is the first time you have mentioned a network.

In fact you said this:

my files are locate in a folder name code in the c drive

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#9 Post by helene » 08 Jul 2014 03:45

ok sorry ... dont you have a solution for me pleasssssssssseeeeee :( :( :( :(

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#10 Post by foxidrive » 08 Jul 2014 04:06

helene wrote:ok sorry ... dont you have a solution for me pleasssssssssseeeeee :( :( :( :(



If you want working code then you have to be honest with the details about the task.

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#11 Post by helene » 08 Jul 2014 04:15

i don't want to write a code i want to facilitate my task by writing a script which will rename the files i will receive.
i already wrote 3 differents scripts that save the files in another folder and the second script rename them but when renaming instead of deleting the 1st number it replace it by space which is considering as a character and the third script send them to the application. so i want the second script that will delete directly the 1st number and not replacing it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#12 Post by foxidrive » 08 Jul 2014 04:54

code = script

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#13 Post by helene » 08 Jul 2014 05:08

ok... :?: :?: :?: :?: :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: rename a file in a batch script

#14 Post by foxidrive » 08 Jul 2014 05:18

helene wrote:excuse me foxidrive my files are locate in a folder name code in the c drive how can i directorate your script to work with my folder????



Try the script in c:\code with some .txt files inside.

helene
Posts: 9
Joined: 07 Jul 2014 02:29

Re: rename a file in a batch script

#15 Post by helene » 08 Jul 2014 06:41

i put the code in the same directory as my files it is not working...the command prompt " file not found..."

Post Reply