Sign in to follow this  
Followers 0
Duck

Logic help with script to read files in folder, create directory for each file and move file into directory

4 posts in this topic

#1 ·  Posted (edited)

I am attempting to create a script to read all the files in a folder, created a subdirectory in the folder named after the file and move the file into the newly created folder. I am having some issues coming up with the logic and i would really appreciate some guidance on this. Below is what ive come up with so far however when i create a directory i only make one folder and it's name is literally the number of files in the array. When i attempt to add 1 to $i ($i = +1) to get the files name instead of the number of files it does not make the directory at all.    

 

For example, if i have a folder "A" that contains 20 text files, I want the script to read the text file, create a directory named the same as the text file and then move the text file into the newly created folder. 

 

#include <file.au3>
#include <Array.au3>

Global $sPath = @ScriptDir
Global $filesArray = _FileListToArray($sPath , "*.txt")

For $i In $filesArray
    DirCreate(@ScriptDir & '\' & $filesArray[$i])
    ;FileMove($filesArray, @ScriptDir & $filesArray & '\')

Next

 

Edited by Duck

Share this post


Link to post
Share on other sites



You're pretty close, the problem with creating a folder with the exact same name as the file is Windows won't allow it, because the name already exists in the path. You could remove the .txt and create a folder without the extension

#include <file.au3>
#include <Array.au3>

Global $sPath = @ScriptDir & '\'
Global $filesArray = _FileListToArray($sPath , "*.txt")

For $i = 1 to $filesArray[0]
    Local $dirName = StringTrimRight($filesArray[$i], 4)
    DirCreate($sPath & $dirName)

    FileMove($sPath & $filesArray[$i], $sPath & $dirName & '\' & $filesArray)
Next

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Or if the folder name must be identicle to the file name - including extension - and/or you deal with files which do not have a Dot-3 styled file extension. You can rename the file temporarily, before you create the directory.

 

Using InunoTaishou's example as a base:

#include <file.au3>
#include <Array.au3>

Global $sPath = @ScriptDir & '\'
Global $filesArray = _FileListToArray($sPath, "*", 1)

For $i = 1 to $filesArray[0]
    FileMove($sPath & $filesArray[$i], $sPath & $filesArray[$i] & '_')
    DirCreate($sPath & $filesArray[$i])
    FileMove($sPath & $filesArray[$i] & '_', $sPath & $filesArray[$i] & '\' & $filesArray[$i])
Next

 

Edited by hydranix

Share this post


Link to post
Share on other sites

Thank you very much! This works perfectly! 

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  
Followers 0