Script for finding a folder and move files

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Script for finding a folder and move files

Post by MigrationUser »

04 Dec 2008 09:13
metropol

Hi

I need a script that search for a specific folder on the e:\ drive and when it is found, the files in it should be moved up to the parent directory. Then delete the folder.

Ex

c:\<parent>\music\<files here>

find music

move all files in music to parent directory

delete music

appreciate the effort

----------------------------

#2 04 Dec 2008 11:51
bluesxman


I've not tested this, but I reckon it should do what you're after:

Code: Select all

@echo off

set "parent=c:\someDirectory"
set "target=music"

for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d "%parent%\%target%"`) do (
    move "%%~a\*" "%parent%"
)
NB -- only files sitting directly under %target% will be moved. Anything in sub-directories will not (unless they're also sitting in a directory called music).

If you wanted to "flatten" the folder structure (IE bring all files in subdirectories as well) then you could do something like this (also untested):

Code: Select all

@echo off

set "parent=c:\someDirectory"
set "target=music"

for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d "%parent%\%target%"`) do (
    for /f "usebackq tokens=*" %%b in (`dir /b/s/a:d "%%~a"`) do (
        move "%%~b\*" "%parent%"
    )
)
Last edited by bluesxman (04 Dec 2008 11:52)

cmd | *sh | ruby | chef

----------------------------

#3 04 Dec 2008 13:25
metropol


Thank you. Great work. I will test this right away.

I have spent many-many hours copying files using the windows explorer, but suddently I disovered the "old-fashion-technology" DOS promt, and found that this is actually a much faster way of doing heavy jobs.

Best regards

----------------------------

#4 04 Dec 2008 13:40
metropol


One stupid qustion. I actual have problems with the ~(tilde) symbol. When I press 'Alt gr', which is necessary for norwegian keyboards, the dos editor highlight the menues. And I'm not able to choose the ~sign.

----------------------------

#5 04 Dec 2008 13:43
metropol


Found the answer use Ascii (Alt + 0126). cool

----------------------------

#6 04 Dec 2008 14:04
metropol


I get an error message saying that "The filename, directory name, or volume label syntax is incorrect"

Strange. I have double checked. Seems to be right.

----------------------------

#7 04 Dec 2008 18:03
bluesxman


Hmmm yeah that method seems to be flawed.

Try this instead -- I've actually been arsed to test this one smile

Code: Select all

@echo off

set "parent=c:\someDirectory"
set "target=music"

for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d "%parent%"`) do (
    echo:"%%~a\" | findstr "\\%target%\\" >nul && move "%%~a\*" "%parent%"
)
pause
cmd | *sh | ruby | chef

----------------------------

#8 05 Dec 2008 13:45
metropol


Better. It goes through each file, but I got the error message.
FINDSTR: Cannot open %
FINDSTR: Cannot open move

----------------------------

#9 06 Dec 2008 21:26
metropol


I have done some checking, and it seems that the batch have problems with file and foldernames which is over 8 characters or have space in the filename Ex:"Laure Bristol" will result in an error. Only "Laure" seems ok.

----------------------------

#10 09 Dec 2008 00:54
avery_larry


findstr might need the spaces escaped somehow. That should be fun. Perhaps:

findstr "\\%target: =/ %\\"

Note there's a space after the : and a space between the / and %

Not tested. May have to be a backslash instead of a slash.

Also, make sure you're running in cmd not command, as the spaces will not be supported in "old" dos (limited to 8.3 support).

That would REALLY be fun to manage.

----------------------------

#11 09 Dec 2008 22:24
metropol
avery_larry wrote:

findstr might need the spaces escaped somehow. That should be fun. Perhaps:

findstr "\\%target: =/ %\\"

Note there's a space after the : and a space between the / and %

Not tested. May have to be a backslash instead of a slash.

Also, make sure you're running in cmd not command, as the spaces will not be supported in "old" dos (limited to 8.3 support).

That would REALLY be fun to manage.
Will give it a try when I'm back home. No I have to finish this weeks project work.

Thanks avery_larry

----------------------------

#12 12 Dec 2008 02:51
avery_larry


Actually, I found that the code seems to work even with long names, at least using cmd.exe.

I do get the error message you describe, but it's only when the folder that you're searching for (music) is empty.

----------------------------

#13 12 Dec 2008 05:00
carlos

Code: Select all

@echo off

set "parent=c:\someDirectory"
set "target=music"

for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d "%parent%"`) do (
    echo:"%%~a\" | findstr "\\%target%\\" >nul && move "%%~a\*" "%parent%" >nul 2>&1 
)
pause

solution: >nul 2>&1
----------------------------

#14 12 Dec 2008 05:11
carlos


Other option:

Code: Select all

@echo off

set "directory=c:\someDirectory\music"

for %%a in ("%directory%") do (move "%%~a\*" "%%~a\.." >nul 2>&1)

pause
Last edited by carlos (12 Dec 2008 05:12)
Post Reply