ethan_hines

REQ: A script to rename all files in a directory to remove spaces/other non standard charaters

6 posts in this topic

This is what I have so far

$tabNomsImages = _FileListToArray("OEBPS\images\", "*")
For $i=1 to UBound($tabNomsImages) - 1

   If StringRight($tabNomsImages[$i], 4) = ".jpg" Then
    $nomImage = $tabNomsImages[$i]
    ConsoleWrite ("Image File Name =" & $nomImage & @LF)
;Replace spaces of the filename of the image with underscores as the case may be
;   $nomImage = StringReplace($nomImage," ","_")
      If StringIsSpace ($nomImage) Then
          ConsoleWrite ("Yes there are spaces in this filename") ;*SEE OUTPUT BELOW!
     Else
          ConsoleWrite ("There are NO spaces in this filename - ")
      If StringIsDigit(StringLeft($nomImage, 1)) Then
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","-")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      Else
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
;Replace spaces and apostoprohes and "&" of the filename with underscores as the case may be
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","_")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      EndIf
      EndIf
   EndIf
Next

This is the output from the console......

There are NO spaces in this filename - Image File Name =Atelier10-RafaeleGermain-7.jpg
There are NO spaces in this filename - Image File Name =Atelier10-UQAMRafaeleGermain-6.jpg
There are NO spaces in this filename - Image File Name =c1.jpg
There are NO spaces in this filename - Image File Name =logo atelier.jpg  ***UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo canada.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo conseil.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo document.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo sodec.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logoAC.jpg
There are NO spaces in this filename - Image File Name =LogoBCorp_fr.jpg
There are NO spaces in this filename - Image File Name =logos.jpg
There are NO spaces in this filename - Image File Name =logo_atelier10.jpg
There are NO spaces in this filename - Image File Name =RBG-D10-01_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-02_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-03_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-04_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-05_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-06b_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-07_Page_GARDE__DCS_02.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-07_Page_GARDE__DCS_021.jpg
There are NO spaces in this filename - +>08:15:30 AutoIt3.exe ended.rc:0
+>08:15:30 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 3.227

1st. Why is it giving me false information?
2nd. If there is a space how do I remove it I saw something about

FileMove(@ScriptDir & "\*.mp3", @ScriptDir & "MP3file- " & ... [date & time] ... & ".mp3", 8)

Should I use this function somehow?

I appreciate any help you can give a newbie who had to take over for the regular programmer. Thanks.

Share this post


Link to post
Share on other sites



Quote

StringIsSpace

Checks if a string contains only whitespace characters.

You could probably do it all with StringRegExpReplace in one operation, instead of 4 lines.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Global $ID_IMG
; Script Start - Add your code below here
$tabNomsImages = _FileListToArray("C:\Users\rootx\Desktop\Nuova cartella", "*")
For $i=1 to UBound($tabNomsImages) - 1

   If StringRight($tabNomsImages[$i], 4) = ".jpg" Then
    $nomImage = $tabNomsImages[$i]
    ConsoleWrite ("Image File Name =" & $nomImage  &@CRLF)
;Replace spaces of the filename of the image with underscores as the case may be
;   $nomImage = StringReplace($nomImage," ","_") 
      if StringLen($nomImage) > StringLen(StringStripWS($nomImage,8)) Then ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          ConsoleWrite ("Yes there are spaces in this filename"&@CRLF) ;*SEE OUTPUT BELOW!
      Else
          ConsoleWrite ("There are NO spaces in this filename - "&@CRLF)
      If StringIsDigit(StringLeft($nomImage, 1)) Then
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","-")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      Else
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
;Replace spaces and apostoprohes and "&" of the filename with underscores as the case may be
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","_")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      EndIf
      EndIf
   EndIf
Next

:)

Share this post


Link to post
Share on other sites
19 hours ago, ethan_hines said:

2nd. If there is a space how do I remove it I saw something about

Do you want to remove it or replace it with an underscore?
 

Quote

 

FileMove(@ScriptDir & "\*.mp3", @ScriptDir & "MP3file- " & ... [date & time] ... & ".mp3", 8)

Should I use this function somehow?

 

Yes, see example #2 in the help file for FileMove

What is $ID_IMG?

Can you post a representative example of the file list?

kylomas


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites
On 2016-11-05 at 5:21 AM, kylomas said:

Do you want to remove it or replace it with an underscore?
Ideally replace it with an underscore

Yes, see example #2 in the help file for FileMove

What is $ID_IMG?

$ID_IMG is just a the name of a image (again without spaces or "weird caracters" ie. %@$#!

Can you post a representative example of the file list?

Yes:

Image File Name =Atelier10-Coll-2--RafaeleGermain.jpg
Image File Name =Atelier10-RafaeleGermain-7.jpg
Image File Name =Atelier10-UQAMRafaeleGermain-6.jpg
Image File Name =c1.jpg
Image File Name =logo atelier.jpg ---This file needs to be changed to logo_atelier.jpg
Image File Name =logo canada.jpg--This file needs to be changed to logo_canada.jpg
Image File Name =logo conseil.jpg----This file needs to be changed to logo_conseil.jpg
Image File Name =logo document.jpg--This file needs to be changed to logo_document.jpg
Image File Name =logo sodec.jpg--This file needs to be changed to logo_sodec.jpg
Image File Name =logoA%C.jpg--This file needs to be changed to logoA_C.jpg
Image File Name =LogoBCorp_fr.jpg
Image File Name =logos.jpg
Image File Name =logo_atelier10.jpg
Image File Name =RBG-D10-01_D&CS.jpg----This file needs to be changed to RBG-D10-01_D_CS.jpg
Image File Name =RGB-D10-02_D&CS.jpg----This file needs to be changed to RBG-D10-02_D_CS.jpg
Image File Name =RGB-D10-03_D&CS.jpg----This file needs to be changed to RBG-D10-03_D_CS.jpg
Image File Name =RGB-D10-04_D&CS.jpg----This file needs to be changed to RBG-D10-04_D_CS.jpg
Image File Name =RGB-D10-05_D&CS.jpg----This file needs to be changed to RBG-D10-05_D_CS.jpg
Image File Name =RGB-D10-06b_D&CS.jpg----This file needs to be changed to RBG-D10-06b_D_CS.jpg
Image File Name =RGB-D10-07_Page_GARDE__DCS_02.jpg
Image File Name =RGB-D10-07_Page_GARDE__DCS_021.jpg

kylomas

If you need any more info please let me know.

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

On 11/4/2016 at 9:25 AM, ethan_hines said:

This is what I have so far

$tabNomsImages = _FileListToArray("OEBPS\images\", "*")
For $i=1 to UBound($tabNomsImages) - 1

   If StringRight($tabNomsImages[$i], 4) = ".jpg" Then
    $nomImage = $tabNomsImages[$i]
    ConsoleWrite ("Image File Name =" & $nomImage & @LF)
;Replace spaces of the filename of the image with underscores as the case may be
;   $nomImage = StringReplace($nomImage," ","_")
      If StringIsSpace ($nomImage) Then
          ConsoleWrite ("Yes there are spaces in this filename") ;*SEE OUTPUT BELOW!
     Else
          ConsoleWrite ("There are NO spaces in this filename - ")
      If StringIsDigit(StringLeft($nomImage, 1)) Then
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","-")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      Else
        $nomID = $ID_IMG & StringMid($nomImage, 1, StringLen($nomImage) - 4)
;Replace spaces and apostoprohes and "&" of the filename with underscores as the case may be
        $nomID = StringReplace($nomID," ","_")
        $nomID = StringReplace($nomID,"'","_")
        $nomID = StringReplace($nomID,"’","_")
        $nomID = StringReplace($nomID,",","_")
      EndIf
      EndIf
   EndIf
Next

This is the output from the console......

There are NO spaces in this filename - Image File Name =Atelier10-RafaeleGermain-7.jpg
There are NO spaces in this filename - Image File Name =Atelier10-UQAMRafaeleGermain-6.jpg
There are NO spaces in this filename - Image File Name =c1.jpg
There are NO spaces in this filename - Image File Name =logo atelier.jpg  ***UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo canada.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo conseil.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo document.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logo sodec.jpg **UM YEAH THERE IS***
There are NO spaces in this filename - Image File Name =logoAC.jpg
There are NO spaces in this filename - Image File Name =LogoBCorp_fr.jpg
There are NO spaces in this filename - Image File Name =logos.jpg
There are NO spaces in this filename - Image File Name =logo_atelier10.jpg
There are NO spaces in this filename - Image File Name =RBG-D10-01_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-02_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-03_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-04_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-05_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-06b_DCS.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-07_Page_GARDE__DCS_02.jpg
There are NO spaces in this filename - Image File Name =RGB-D10-07_Page_GARDE__DCS_021.jpg
There are NO spaces in this filename - +>08:15:30 AutoIt3.exe ended.rc:0
+>08:15:30 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 3.227

1st. Why is it giving me false information?
2nd. If there is a space how do I remove it I saw something about

FileMove(@ScriptDir & "\*.mp3", @ScriptDir & "MP3file- " & ... [date & time] ... & ".mp3", 8)

Should I use this function somehow?

I appreciate any help you can give a newbie who had to take over for the regular programmer. Thanks.

1.) I would recommend FileListToArrayRec() just incase you have folders inside of your folder.

2.) Looks like your checking every string to see if it ends in .jpg because you only want .jpg files, if that is the case why not use *.jpg in your FileList to cut down code and probably make it run faster.  Better yet also specify FilesOnly not Files/Folders as the default. 

3.) As far as I can tell the last If statement says If This Then StringReplace() Else StringReplace() looks like you just doubling the code to do the same thing regardless of the outcome of the if statement.

I would definitely roll up your replace statements with RexExpReplace() and did you want to replace the characters with an underscore (your code) or delete them (your title)

 

This is what I came up with, works for me as far as I can tell, if its not exactly what you want should be close enough for you to work with it.

#Include <Array.au3>
#Include <File.au3>

Local $sDirectory = @ScriptDir & "\Test"
Local $aFilesJpg = _FileListToArrayRec($sDirectory, "*.jpg", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;.JPG Only
Local $aFilesNameJpg = _FileListToArrayRec($sDirectory, "*.jpg", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_NOPATH) ;.JPG Only

If IsArray($aFilesJpg) Then
For $i = 1 to $aFilesJpg[0]
    If StringRegExp($aFilesJpg[$i], '(?i)[\s''’"]') Then
        ConsoleWrite("File " & $aFilesJpg[$i] & " matches replacement testing and will be processed")
        $sProsessed = StringRegExpReplace($aFilesNameJpg[$i], '(?i)[\s''’"]', "_")
        Run(@ComSpec & " /c REN " & '"' & $aFilesJpg[$i] & '" "' & $sProsessed & '"')
    EndIf
Sleep(10)
Next
EndIf

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now