Jump to content
Sign in to follow this  

Remove spaces from directory stucture

Recommended Posts

I need to replace spaces in our directory structure with underscores. The script below is close, but once it renames the first directory, it fails. I think it's still using the variable value prior to when the first directory gets renamed and since it can't find it any longer, it fails.

Any tips on how to get this working, or if there's a better way?


Global $cCollection, $oFSO, $oItem, $oFolder
Global $cFiles

Dim $TopFolder, $Path, $Name, $NameLen, $NewName, $NumberReplaced, $DirMove

$TopFolder = (@WorkingDir & "\Test\")

$oFSO = ObjCreate('Scripting.FileSystemObject')

$oFolder = $oFSO.GetFolder($TopFolder)

$cFiles = $oFolder.Files

For $oFile in $cFiles
Func RemoveSpaces($oFolder)

 If Not @error Then  

  $cCollection = $oFolder.SubFolders

  For $oItem In $cCollection
   ;msg("$oItem.Path = " & $oItem.Path)
   ;msg("$oItem.Path = " & $oItem.Name)  
   $Name = $oItem.Name
   $Path = $oItem.Path   
   $NameLen = StringLeft($path,StringLen($path)-StringLen($Name))
   ;msg("$NameLen = " & $NameLen)
   $cFiles = $oItem.Files
   ;msg("$Name = " & $Name)
   If StringInStr($Name, " ") Then 

    $NewName = StringReplace($Name, " ", "_")
    $NumberReplaced = @extended
    $DirMove = DirMove($oItem.Path, $NameLen & "\" & $NewName)   
   msg("$DirMove = " & $DirMove & "    1 is successfull" & @CRLF _
    & $NumberReplaced & "spaces replaced")
   msg("$NewName = " & $NewName)

 ElseIf @error Then

EndFunc ;RemoveSpaces()

Func msg($msg) 
MsgBox(0, "msgbox", $msg)
Edited by stev379

Share this post

Link to post
Share on other sites

Try this:

#include <Constants.au3>

$TopFolder = (@WorkingDir & "\Test\")

$foo = Run(@ComSpec & " /c dir /s /b", $TopFolder, @SW_HIDE, $STDOUT_CHILD)
$output = ""
While 1
    $output &= StdoutRead($foo)
    If @error Then ExitLoop
$output = StringStripWS($output,3)  
$aDirs = stringsplit($output,@crlf,1)

for $i = $aDirs[0] to 1 step -1 ; we have to do the subdirectories first
    $dir = $aDirs[$i]
    if stringinstr(FileGetAttrib($dir),"D") Then
        $slashloc = StringInStr($dir,"\",0,-1)
        $dirname = stringmid($dir,$slashloc+1)
        $parentname = stringleft($dir,$slashloc)
        $newdir = $parentname & StringReplace($dirname," ","_")
        DirMove ($dir,$newdir)

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post

Link to post
Share on other sites

Hello lod3n,

Your script function very well. But I would like you explain each line.

I can tell you, your script functioned for certain files once, but after he didn't function ???

Thank you in advance.

Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...