Copying?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
arilotter
Posts: 2
Joined: 26 Aug 2010 11:14

Copying?

#1 Post by arilotter » 26 Aug 2010 18:27

I have a folder full of .png files, and i was wondering how i would go about creating a code such as this pseudocode:

Code: Select all

:start
find first png
create directory (pngname)
move png to directory
rename png in directory to icon.png
if number of pngs > 0
goto start
else
exit

Sorry if this is the wrong type of forum for this, i'm kind of a noob at batch scripting because i own a mac. :roll:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Copying?

#2 Post by orange_batch » 26 Aug 2010 21:07

It's the right forum. Easy as pie, buddy.

Code: Select all

@echo off&echo:&color B&title Make folder, move and rename PNGs by orange_batch

for %%x in ("C:\...\Target Folder\*.png") do (
md "%%~dpnx"
move "%%x" "%%~dpnx"
ren "%%~dpnx\%%~nxx" icon.png
echo: ^> Made folder: "%%~nx" Moved: "%%~nxx" Renamed to: "icon.png"
echo:
)

echo:   Press any key to exit.
pause>nul
exit


An explanation:

"for" loops through whatever it accepts for input (default and switches /d /r, /l, /f). If you type "for /?" in the command line, it tells you about "optional syntax" which lets you expand it's (temporary local) variable to specific information. It works on arguments as well, but not regular variables.

For every .png in the target folder, loop through the code block and expand %%x to it's location.

Code: Select all

for %%x in ("C:\...\Target Folder\*.png")


Make directory "Drive:\Path\Current File's Name"

Code: Select all

md "%%~dpnx"


Move current file to "Drive:\Path\Current File's Name"

Code: Select all

move "%%x" "%%~dpnx"


Rename "Drive:\Path\Current File's Name\Current File's Name & Extension" to icon.png

Code: Select all

ren "%%~dpnx\%%~nxx" icon.png

arilotter
Posts: 2
Joined: 26 Aug 2010 11:14

Re: Copying?

#3 Post by arilotter » 26 Aug 2010 21:35

Wow! Thank you so much! So will this create a folder in the same directory as the pngs?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Copying?

#4 Post by orange_batch » 26 Aug 2010 23:47

Yes, but you can modify the code of course. Remember to change C:\...\Target Folder to your, target folder.

If you change:

("C:\...\Target Folder\*.png")

to

("%~1\*.png")

you can instead drag and drop any folder onto the batch file, and it will run the process in any .pngs in that folder.

It could include subfolders, but that would require for /f and dir.
Last edited by orange_batch on 27 Aug 2010 02:10, edited 1 time in total.

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

Re: Copying?

#5 Post by !k » 27 Aug 2010 00:50

orange_batch wrote:move "%%x" "%%~dpnx"
ren "%%~dpnx\%%~nxx" icon.png
is equal move "%%x" "%%~dpnx\icon.png"


arilotter wrote:So will this create a folder in the same directory as the pngs?

Code: Select all

@echo off
setlocal enableextensions

set "Source=d:\path\Source Folder"
set "Target=e:\path\path\Target Folder"

for %%x in ("%Source%\*.png") do (
echo f| xcopy /y "%%x" "%Target%\%%~nx\icon.png" &&del /q "%%x"
)

pause
exit

Post Reply