A few script requests within 1 post [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

A few script requests within 1 post [SOLVED]

#1 Post by Andrius » 26 Jul 2012 09:53

First script
Rip through a folder and subfolder structure finding all "&" special characters and replace them with the text "and" (without the quotes of course)

Second Script
Rip 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.
Last edited by Andrius on 26 Jul 2012 15:02, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A few script requests within 1 post (So I don't spam)

#2 Post by Squashman » 26 Jul 2012 09:57

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.

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: A few script requests within 1 post (So I don't spam)

#3 Post by Andrius » 26 Jul 2012 10:00

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#4 Post by foxidrive » 26 Jul 2012 10:15

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 file
This 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%"
Last edited by foxidrive on 26 Jul 2012 10:56, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#5 Post by foxidrive » 26 Jul 2012 10:32

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%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#6 Post by foxidrive » 26 Jul 2012 10:40

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"
)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A few script requests within 1 post (So I don't spam)

#7 Post by Squashman » 26 Jul 2012 10:43

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

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: A few script requests within 1 post (So I don't spam)

#8 Post by Andrius » 26 Jul 2012 10:44

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?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A few script requests within 1 post (So I don't spam)

#9 Post by Squashman » 26 Jul 2012 10:45

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#10 Post by foxidrive » 26 Jul 2012 10:48

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
)

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: A few script requests within 1 post (So I don't spam)

#11 Post by Andrius » 26 Jul 2012 10:50

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#12 Post by foxidrive » 26 Jul 2012 10:52

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.

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: A few script requests within 1 post (So I don't spam)

#13 Post by Andrius » 26 Jul 2012 10:55

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!!!!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: A few script requests within 1 post (So I don't spam)

#14 Post by foxidrive » 26 Jul 2012 11:03

You're welcome, and thanks to Squashman too.

Note that some files/folders may fail to rename if they contain % characters.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: A few script requests within 1 post (So I don't spam)

#15 Post by Squashman » 26 Jul 2012 11:07

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

Post Reply