Page 1 of 1

Copying?

Posted: 26 Aug 2010 18:27
by arilotter
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:

Re: Copying?

Posted: 26 Aug 2010 21:07
by orange_batch
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

Re: Copying?

Posted: 26 Aug 2010 21:35
by arilotter
Wow! Thank you so much! So will this create a folder in the same directory as the pngs?

Re: Copying?

Posted: 26 Aug 2010 23:47
by orange_batch
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.

Re: Copying?

Posted: 27 Aug 2010 00:50
by !k
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