Page 1 of 2
A few script requests within 1 post [SOLVED]
Posted: 26 Jul 2012 09:53
by Andrius
First scriptRip through a folder and subfolder structure finding all "&" special characters and replace them with the text "and" (without the quotes of course)
Second ScriptRip through a folder and subfolder structure and remove ALL special characters from the filenames "!@#$%^&*()_+';:",./<>?`~. This should not leave a space where the special character was just simply remove the character.
Code: Select all
Eg: BEFORE: file&name.psd AFTER: filename.psd
Third Script:Rip through a folder and subfolder structure and ensure that each FILE (not folder) has a set prefix_prefix_prefix_ in front of the filename
Code: Select all
Eg: BEFORE: filename.psd AFTER prefix1_prefix2_prefix3_filename.psd
(information is sensitive so I have simply left it as prefix1_prefix2_prefix3_ )
Final Script:Rip through a folder and subfolder structure and find folders that have both a "model" and "oldmodel" folder within the folder then output a text file with a list of these offenders OR copy the root folder of these folders somewhere so that I can go in and look at them. Confusing so I will give example below.
Code: Select all
[color=#40BF40]This is the ideal folder structure[/color]
c:\Item1
c:\Item1\animation
c:\Item1\export
c:\Item1\model
c:\Item1\source
c:\Item1\texture
[color=#FF0000]This would be an offending structure that I need brought to my attention somehow (via list or copied somewhere)[/color]
c:\Item1
c:\Item1\animation
c:\Item1\export
[b]c:\Item1\model
c:\Item1\oldmodel[/b]
c:\Item1\source
c:\Item1\texture
Notice that this offending folder has BOTH model & oldmodel. If a folder has only 1 of these it is fine but if it contains BOTH of these folders I need it brought up.
That's it! As always you guys have my eternal thanks and Im learning a lot by looking at how you compile these scripts... not to mention you are saving me days of man hours with these scripts as I am looking at thousands and thousands of folders here.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 09:57
by Squashman
It has been said on this forum plenty of times that these characters \ / : * ? " < > | cannot exist in file names. How did you get them in your file names. Windows shouldn't let you do that.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:00
by Andrius
My mistake on those characters I just pasted in as many special characters that I could find on my keyboard quickly to get the general idea across that I can't have any special characters whatsoever in the names.
You have my apologies for any confusion that this may have caused.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:15
by foxidrive
First script: Test all my scripts on sample folders and files first.
This is to convert "&" to "and" and NOT " & " to " and "Run this script first to rename folders, to create renitems.txt and rename it to .bat and run it if it looks right.
Code: Select all
@echo off
del renitems.txt 2>nul
for /f "delims=" %%a in ('dir /a:d /o:n /b /s ^|sort /r') do call :next "%%a"
GOTO:EOF
:next
set "var=%~nx1"
set "var2=%var:&=and%"
if not "%var%"=="%var2%" >> renitems.txt echo ren %1 "%var2%"
You may need to run the above several times until it does not create a new output fileThis is because if a folder gets renamed and it contains further folders with & in it then the subsequent renames will fail (harmlessly), as the path has changed.
EDIT: Added the sort command so it may rename all folders on a single pass.Run this script second after the folders have been renamed and this is to rename the files, to create renitems2.txt and rename it to .bat and run it if it looks right.
Code: Select all
@echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b /s') do call :next "%%a"
GOTO:EOF
:next
set "var=%~nx1"
set "var2=%var:&=and%"
if not "%var%"=="%var2%" >> renitems2.txt echo ren %1 "%var2%"
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:32
by foxidrive
Script 2:
This will create renitems3.txt to remove special characters. Examine it and if it looks right rename it to .bat and run it.
Note that you can't replace these characters easily using that technique = ~ % and foreign language characters
Also note that special characters not listed below will not be changed.
Code: Select all
@echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b /s') do call :next "%%a"
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname:)=%"
set "newname=%newname:(=%"
set "newname=%newname:&=%"
set "newname=%newname:^=%"
set "newname=%newname:$=%"
set "newname=%newname:#=%"
set "newname=%newname:@=%"
set "newname=%newname:!=%"
set "newname=%newname:-=%"
set "newname=%newname:+=%"
set "newname=%newname:}=%"
set "newname=%newname:{=%"
set "newname=%newname:]=%"
set "newname=%newname:[=%"
set "newname=%newname:;=%"
set "newname=%newname:'=%"
set "newname=%newname:`=%"
set "newname=%newname:,=%"
if not "%~nx1"=="%newname%" >> renitems3.txt echo ren %1 "%newname%
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:40
by foxidrive
Third script:
This will create renitems4.txt with rename commands to prepend "prefix_prefix_prefix_" to all files that don't have it.
If any foldernames start with "prefix_prefix_prefix_" then it will skip checking files.
Code: Select all
@echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b /s ^| findstr /v /i "\prefix_prefix_prefix_"') do (
>> renitems4.txt echo ren "%%a" "prefix_prefix_prefix_%%~nxa"
)
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:43
by Squashman
Foxidrive,
Pipe your dir command to the sort command.
This will float the longest directory name to the top within a folder structure.
Code: Select all
H:\Downloads>dir /a:d /o:n /b /s
H:\Downloads\Albums
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads>dir /a:d /o:n /b /s | sort /r
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:44
by Andrius
Squashman wrote:Foxidrive,
Pipe your dir command to the sort command.
This will float the longest directory name to the top within a folder structure.
Code: Select all
H:\Downloads>dir /a:d /o:n /b /s
H:\Downloads\Albums
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads>dir /a:d /o:n /b /s | sort /r
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums
Should I wait for this script to be polished or go with the original?
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:45
by Squashman
Andrius wrote:Squashman wrote:Foxidrive,
Pipe your dir command to the sort command.
This will float the longest directory name to the top within a folder structure.
Code: Select all
H:\Downloads>dir /a:d /o:n /b /s
H:\Downloads\Albums
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads>dir /a:d /o:n /b /s | sort /r
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums
Should I wait for this script to be polished or go with the original?
Well you could add the code yourself.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:48
by foxidrive
Fourth script:
This will notify you in items5.txt of any folders containing both model and oldmodel folders.
Code: Select all
@echo off
set "loc=%cd%"
for /f "delims=" %%a in ('dir /a:d /o:n /b /s') do (
pushd "%%a"
if exist "model\" if exist "oldmodel\" >> "%loc%\items5.txt" echo "%%a" has model and oldmodel folders
popd
)
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:50
by Andrius
Squashman wrote:Andrius wrote:Squashman wrote:Foxidrive,
Pipe your dir command to the sort command.
This will float the longest directory name to the top within a folder structure.
Code: Select all
H:\Downloads>dir /a:d /o:n /b /s
H:\Downloads\Albums
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads>dir /a:d /o:n /b /s | sort /r
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums
Should I wait for this script to be polished or go with the original?
Well you could add the code yourself.
I would love to but Im pretty much a complete noob just picking this stuff up so Im not sure how to "pipe" these in.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:52
by foxidrive
Squashman wrote:Foxidrive,
Pipe your dir command to the sort command.
This will float the longest directory name to the top within a folder structure.
Code: Select all
H:\Downloads>dir /a:d /o:n /b /s
H:\Downloads\Albums
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads>dir /a:d /o:n /b /s | sort /r
H:\Downloads\Albums\WorkFolder\Album-March
H:\Downloads\Albums\WorkFolder\Album-January
H:\Downloads\Albums\WorkFolder\Album-February
H:\Downloads\Albums\WorkFolder\Album-April
H:\Downloads\Albums\WorkFolder
H:\Downloads\Albums
Thanks Squashman. It world work for alphanumerics but maybe could get confused with foreign characters - Andrius can add it if it's going to be something they use more than once.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 10:55
by Andrius
foxidrive wrote:Fourth script:
This will notify you in items5.txt of any folders containing both model and oldmodel folders.
Code: Select all
@echo off
set "loc=%cd%"
for /f "delims=" %%a in ('dir /a:d /o:n /b /s') do (
pushd "%%a"
if exist "model\" if exist "oldmodel\" >> "%loc%\items5.txt" echo "%%a" has model and oldmodel folders
popd
)
Thank you!!!!!
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 11:03
by foxidrive
You're welcome, and thanks to Squashman too.
Note that some files/folders may fail to rename if they contain % characters.
Re: A few script requests within 1 post (So I don't spam)
Posted: 26 Jul 2012 11:07
by Squashman
Andrius wrote:I would love to but Im pretty much a complete noob just picking this stuff up so Im not sure how to "pipe" these in.
Showed you how to PIPE in a FOR loop in this thread.
viewtopic.php?f=3&t=3554