# Solved: Substring a filename and copy to new folder using dos



## aled_l_d (Mar 16, 2011)

Hi,
I have an image in one folder called DAV01030153_2011031410155796.jpg. What I would like to do is substring this so that it just reads DAV01030153.jpg and then copy this into a new folder. I have no idea how to go about this. I can copy the image as it is from one folder to another using xcopy but its the bit before that gets me baffled.

Any help would be great.

Thanks in advance


----------



## Squashman (Apr 4, 2003)

```
H:\>set filename=DAV01030153_2011031410155796.jpg

H:\>set newname=%filename:~0,11%.jpg

H:\>echo %newname%
DAV01030153.jpg

H:\>
```


----------



## aled_l_d (Mar 16, 2011)

Thanks for that. How would this work for multiples files? How would I pull in each file in turn and then process as shown?


----------



## Squashman (Apr 4, 2003)

If all the files are in a folder you can put the DIR cmd into a For loop.

This should work. Didn't test it. So create some folders to test it with.


```
@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=1* delims=." %%A IN ('dir /b /a-d *.jpg') DO (
     set _Tfile=%%A               .
     set _Filename=!_Tfile:~0,11!
     xcopy  "%%A" "C:\some_other_folder\!_Filename!.jpg"
 )
```


----------



## aled_l_d (Mar 16, 2011)

Thanks Squashman.
Quick question.......
where it states "%%A" do I need to replace this with the folder that holds the original pictures? If not, how does it know where to get the original picture from?

Thanks again


----------



## Squashman (Apr 4, 2003)

%%A is the FOR LOOP variable. 
copy the batch file to the directory you need to execute it in.

If you don't want to put it in the folder that has your pictures then put a change directory or pushd command before the for loop.


----------



## aled_l_d (Mar 16, 2011)

I have put the .bat file into the same folder as the original pictures but nothing happens. This is what the code look like after i have amended it: -

@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=1* delims=." %%A IN ('dir /b /a-d *.jpg') DO (
set _Tfile=%%A .
set _Filename=!_Tfile:~0,11!
xcopy "%%A" "C:\Docs\New_Picture\!_Filename!.jpg"


----------



## Squashman (Apr 4, 2003)

You are missing a closing parenthesis.


----------



## aled_l_d (Mar 16, 2011)

i have tried putting the closing parenthesis after then jpg" and after 11! but nothing. I have also removed the . after %%A to see if it made a differnece but it did not. Any ideas

@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=1* delims=." %%A IN ('dir /b /a-d *.jpg') DO (
set _Tfile=%%A
set _Filename=!_Tfile:~0,11!
xcopy "%%A" "C:\Docs\New_Picture\!_Filename!.jpg")


----------



## Squashman (Apr 4, 2003)

stupid me. I see the problem. I had copied that code from another batch file I have that does something completely different.
I should re-write the batch to use the command modifiers but the quick fix should be to change your xcopy line.

xcopy "%%A.jpg" "C:\Docs\New_Picture\!_Filename!.jpg"


----------



## aled_l_d (Mar 16, 2011)

There is still something that i'm missing. I can't quite put my finger on it. The script currently reads: -

@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=1* delims=." %%A IN (dir /b /a-d *.jpg') DO (
set _Tfile=%%A
set _Filename=!_Tfile:~0,11!
xcopy "%%A.jpg" "C:\Docs\New_Picture\!_Filename!.jpg") 

Do I need to put .jpg after set _Tfile=%%A


----------



## Squashman (Apr 4, 2003)

I could just fix it by fixing the bad code. Just need to change the tokens and remove the delimiters.


```
@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.jpg') DO (
     set _Tfile=%%A
     set _Filename=!_Tfile:~0,11!
     xcopy "%%A" "C:\Docs\New_Picture\!_Filename!.jpg"
 )
```


----------



## Squashman (Apr 4, 2003)

aled_l_d said:


> There is still something that i'm missing. I can't quite put my finger on it. The script currently reads: -
> 
> @echo off & setlocal enableextensions enabledelayedexpansion
> FOR /F "tokens=1* delims=." %%A IN (dir /b /a-d *.jpg') DO (
> ...


No. That is why I changed the xcopy command. Basically the for loop is making the variable equal the filename without the extension. That is what the delimiter is for. It was something that I was using for a different batch file. So we needed to change the xcopy command to put the extension back on to the filename.


----------



## aled_l_d (Mar 16, 2011)

Top man! It works a treat.

Thanks for all the help
Aled


----------



## Squashman (Apr 4, 2003)

But do you understand what each line of the code is doing? I basically gave you the fish instead of teaching you to fish.


----------



## aled_l_d (Mar 16, 2011)

Yes I can see it now. When you see it in front of you in blak and white it makes sense. 

Thanks again


----------



## aled_l_d (Mar 16, 2011)

When I run the .bat file below i am prompted to select a file or directory. I know I can make the destination a directory by either using /I or by placing a \ at the end of the destination directory in the xcopy. Is there a way of forcing it to be a filename rather than a directory?

Here's my code

@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.jpg') DO (
set _Tfile=%%A
set _Filename=!_Tfile:~0,11!
xcopy "%%A" "C:\Docs\Temp_Picture\!_Filename!.jpg"
)
xcopy "C:\Docs\Temp_Picture" "C:\Docs\New_Picture"

Thanks in advance


----------



## TerryNet (Mar 23, 2005)

Merged your threads, as they are on the same topic.


----------



## Squashman (Apr 4, 2003)

Now I am teaching you to fish.
What other commands will do the same task as xcopy.


----------



## aled_l_d (Mar 16, 2011)

Robocopy?


----------



## Squashman (Apr 4, 2003)

You are over thinking. Why not just use copy inside the for loop. That is where you are getting the warning.


----------



## aled_l_d (Mar 16, 2011)

I tried that but it still did not like it. This is what I did

@echo off & setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.jpg') DO (
set _Tfile=%%A
set _Filename=!_Tfile:~0,11!
xcopy "%%A" "C:\Docs\Temp_Picture\!_Filename!.jpg"
xcopy "C:\Docs\Temp_Picture" "C:\Docs\New_Picture"
)


----------



## Squashman (Apr 4, 2003)

The batch file still says XCOPY. I said to use copy. And I know it works because I tested it at work when I wrote the batch file for you because I too saw that warning you were getting when I first tested it. 

You also now have your second xcopy inside the FOR loop. Why are you doing that. You should probably keep that outside the for loop because I am pretty sure you are looking to do a full directory copy.


----------



## aled_l_d (Mar 16, 2011)

Great. I was getting my xcopy mixed up with plain old simple copy.

Thanks again


----------

