Page 1 of 2
rename a file in a batch script
Posted: 07 Jul 2014 06:08
by helene
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.
Re: rename a file in a batch script
Posted: 07 Jul 2014 08:02
by foxidrive
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
Re: rename a file in a batch script
Posted: 07 Jul 2014 11:13
by helene
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????
Re: rename a file in a batch script
Posted: 07 Jul 2014 11:37
by Squashman
Put the batch file in the same directory as the TEXT files and run it.
Re: rename a file in a batch script
Posted: 08 Jul 2014 02:41
by helene
i did it but it show me files not find in the command prompt
Re: rename a file in a batch script
Posted: 08 Jul 2014 02:56
by foxidrive
How many .TXT files are in the same folder as the batch file?
Re: rename a file in a batch script
Posted: 08 Jul 2014 03:02
by helene
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.
Re: rename a file in a batch script
Posted: 08 Jul 2014 03:05
by foxidrive
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
Re: rename a file in a batch script
Posted: 08 Jul 2014 03:45
by helene
Re: rename a file in a batch script
Posted: 08 Jul 2014 04:06
by foxidrive
If you want working code then you have to be honest with the details about the task.
Re: rename a file in a batch script
Posted: 08 Jul 2014 04:15
by helene
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.
Re: rename a file in a batch script
Posted: 08 Jul 2014 04:54
by foxidrive
code = script
Re: rename a file in a batch script
Posted: 08 Jul 2014 05:08
by helene
Re: rename a file in a batch script
Posted: 08 Jul 2014 05:18
by foxidrive
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.
Re: rename a file in a batch script
Posted: 08 Jul 2014 06:41
by helene
i put the code in the same directory as my files it is not working...the command prompt " file not found..."