Add to a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
I_Love_2_DOS
Posts: 1
Joined: 16 Dec 2010 10:39

Add to a file

#1 Post by I_Love_2_DOS » 17 Dec 2010 07:21

How can I add to a file name?

I have a directory called TEST.
Inside TEST there are 200 files (all of different names and extensions).
How can I add “a” in front of each name and still keep the same name and extension. i.e. c:\TEST\hello.txt will become c:\TEST\ahello.txt.
I want to use a batch file.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Add to a file

#2 Post by rfpd » 18 Dec 2010 05:05

i don't think that's possibel but i will see what i can do

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Add to a file

#3 Post by rfpd » 18 Dec 2010 05:13

I found one code that change the first letter to a

Code: Select all

@echo off
rename "C:\Your\Folder\Destination\*.*" a*.*

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Add to a file

#4 Post by ChickenSoup » 21 Dec 2010 14:39

This should do it:

Code: Select all

for /f "tokens=*" %a in ('dir /b') do rename "%a" "a%a"

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Add to a file

#5 Post by rfpd » 23 Dec 2010 17:52

Nope ChickenSoupe that doesn't work.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Add to a file

#6 Post by !k » 24 Dec 2010 08:22

It's designed for command line.
In the batch you must double the %

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Add to a file

#7 Post by Ocalabob » 24 Dec 2010 12:28

The lines below work for me with limited testing. Remove the word ECHO if $$$test.txt gives you the desired results.

Best wishes all.

@echo off
for /f "delims=" %%f in (
'dir /b *.*') do (
set name=%%~nxf
ECHO ren "%%~nxf" "a%%~nxf"
)>>$$$test.txt
set name=

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Add to a file

#8 Post by ChickenSoup » 27 Dec 2010 08:41

ChickenSoup wrote:This should do it:

Code: Select all

for /f "tokens=*" %a in ('dir /b') do rename "%a" "a%a"


Yes, inside a batch file it would be the following:

Code: Select all

for /f "tokens=*" %%a in ('dir /b') do rename "%%a" "a%%a"

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Add to a file

#9 Post by rfpd » 27 Dec 2010 10:45

good job chicken soup it works

Post Reply