1

As I said, I've Some *.Xml Files in one folder, I want to move them to sub-dirs based on their names .

But, I need two more things :-

1- I've to apply/run the command from a parent directory, not from the *.xml directory

2- after making the needed sub-dirs, and moving the xml files into them , I want to move all of them into a new directory called "XML Backup"

For example:- my files located like this

X:\folder1\folder2\file1.xml
X:\folder1\folder2\file2.xml
X:\folder1\folder2\file3.xml

what I need to do is exactly like this:-

X:\folder1\folder2\xml_Backup\file1\file1.xml
X:\folder1\folder2\xml_Backup\file2\file2.xml
X:\folder1\folder2\xml_Backup\file1\file3.xml

please keep in mind, that I HAVE to run the command from X:\folder1

by the way, I've tried to use this command from a batch file in different ways, but I couldn't do it because of the conditions of what I need to do:-

for /R %%F in (*.xml) do (
2>nul md "%%~nF"
>nul Move /y "%%~nF.*" "%%~nF"
)

If you tried to run this batch/command from the *.xml directory, it will make the Sub-Dirs correctly (based on the xml files names), and it'll move the xml files into them , but the problem is after making the new dirs, I need to move these xml dirs into another directory named "Xml Backup".

  • 1
    What version of Windows is this for? Probably doable in batch, but maybe easier in PowerShell if 7+. Also, it seems like you're thinking about this all backwards. You should make the XML_Backup folders and then make the file-specific folders as sub-directories of that, and then move the files. – Iszi Oct 8 '14 at 19:34
  • Also, you only specify that you hare files in X:\folder1\folder2. Are there any deeper sub-directories? Are there any other subfolders or files in X:\folder1 that you do or don't want acted upon? – Iszi Oct 8 '14 at 19:39
1

the problem is after making the new dirs, I need to move these xml dirs into another directory named "Xml Backup"

Modify your batch to create the "xml_Backup" folder (if it doesn't exist), and then include that in your target paths for the MD and Move commands in the loop.

Something like:

rem Change to folder specified as argument.
pushd %1

rem Check if 'backup' folder exists; if not, create it.
if not exist "xml_Backup" md "xml_Backup"

rem Loop through files in the current folder.
for /R %%F in (*.xml) do (
  rem Create subfolder in 'backup' folder matching this file's name.
  md "xml_Backup\%%~nF"
  rem Move file to that folder.
  move /y "%%~nF.*" "xml_Backup\%%~nF"
)

rem Change back to the original directory we were in when pushd was executed.
popd

Run with batchname.bat "c:\folderContainingXMLs".

IE: batchname.bat "X:\folder1\folder2" or batchname.bat ".\folder2", etc.

| improve this answer | |
  • Thanks Ƭᴇcʜιᴇ007 For your help, that's the first step of the solution . like I tried to explain, I have to run the batch from a parent directory, and your modification (which is good) only works correctly if I applied the batch from the same directory where the *.xml files located. But If I tried to apply your batch from the parent directory, it will make all of the needed dirs, but the xml files will not be moved from their location, to these new Dirs. the only problem now, I guess, is that the new Dirs created in the parent dir, not in the *.xml dir. so, any more suggestions? Thanks - Karim – KorkOoO Oct 9 '14 at 12:43
  • @ENG.KARIM I modified it a bit to allow you to specify the folder to process. I'm sure you can tweak it from here. ;) – Ƭᴇcʜιᴇ007 Oct 9 '14 at 13:42
0

If PowerShell is an option, you could try this. Do it on some test data first though - I haven't tested it myself yet.

New-Item X:\folder1\folder2\xml_backup -Type Directory
Get-ChildItem X:\folder1\folder2\*.xml |
ForEach-Object {
    $XMLName = $_.BaseName
    New-Item "X:\folder1\folder2\xml_backup\$XMLName" -Type Directory
    Move-Item $_ "X:\folder1\folder2\xml_backup\$XMLName"
}

Your biggest problem, I think, is that you're approaching the issue backwards. You want to:

  1. Create file-specific folders.
  2. Move the files into their specific folders.
  3. Create an XML Backup folder.
  4. Move the file-specific folders, and their contents, into XML Backup.

What you should be doing, as done in the above script, is:

  1. Create the XML Backup folder.
  2. Create file-specific folders within the XML Backup folder.
  3. Move the files into their appropriate subfolder.

Additional modifications will be needed to the above script, if you want it to run on more than just the XML files in X:\folder1\folder2 (for example, other XML files in adjacent subfolders).

| improve this answer | |
  • Thanks Iszi for your reply.. unfortunately, I can't use PowerShell for this, because what I'm asking about, is just 1% of a big batch script, and all of its commands are working great, except for that one I'm asking about. I've tried to make some modifications to that batch, to fix my problem, but every time I fix something, I lose another!! so I picked the easiest part to ask about. anyway thank you for trying to help, and By the way, I'm Using Win.7- x64 - karim – KorkOoO Oct 9 '14 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.