Duck Posted January 10, 2016 Share Posted January 10, 2016 (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 January 10, 2016 by Duck Link to comment Share on other sites More sharing options...
InunoTaishou Posted January 10, 2016 Share Posted January 10, 2016 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 Duck 1 Link to comment Share on other sites More sharing options...
hydranix Posted January 10, 2016 Share Posted January 10, 2016 (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 January 10, 2016 by hydranix Link to comment Share on other sites More sharing options...
Duck Posted January 10, 2016 Author Share Posted January 10, 2016 Thank you very much! This works perfectly! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now