Move folder/file one layer up

Post any Bulk Rename Utility support requirements here. Open to all registered users.

Move folder/file one layer up

Postby andro299 » Tue Sep 11, 2018 5:19 pm

How do you do it? I cant get it to work with the windows/cmd way (..\).
I would do it manually, but i would have to repeat this 224 times + do this every ~3 months, making it simpler for me to just see if anything can be done automatically.

If anyone wanders what exactly i want this for, this would be one of the directories:
"E:\OverwatchVoiceLines\BASICS\Renamed\Ana\VoiceLine\Anniversary\Common\Follow me"
Each folder on the lowest level contains one sound file, that gets its name from the folder it's in. My objective is to move the sound file into the "Anniversary" folder (example, there are folders on the same level with different names), removing the common and the parent folder. The last folder i would remove from the directory would be the voiceline one.

So in a perfect world, the above directory would turn into this:
"E:\OverwatchVoiceLines\BASICS\Renamed\Ana\Anniversary"

Appretiate any answer :D
andro299
 
Posts: 5
Joined: Tue Sep 11, 2018 5:14 pm

Re: Move folder/file one layer up

Postby andro299 » Sun Sep 23, 2018 6:22 pm

This is what i came up with on my own. If you want to use it for yourself, change the file type (.ogg for me), and the value compared to the counter (for me 6) - you can see for yourself how much you need by counting. For me it was: E: (1) \OverwatchVoiceLines (2) \BASICS (3) \Renamed (4) \Ana (5)\Anniversary (6). It just counts down the number of layers it remembers until it doesnt give a crap about the rest. So if you want to move all .txt files 3 directories up, you set the type to txt and set the compared number to: total layers-number of layers you want to skip.

NOTE: I am not responsible for any mishaps this may cause. Use at your own risk. (You can comment the move command by putting :: in the front and you can echo the two directories intead to see if its correct.)

Code: Select all
@echo off
setlocal enabledelayedexpansion
for /R %%A IN (*.ogg) do (
call set hisDir=%%A
call :getDir %%A
)
pause

:getDir
set /a count=0
call set myDir=%%~d1
call set file=%%~1%%~n1
call set PARENT_DIR=%%~p1
set PARENT_DIR=%PARENT_DIR: =_%
set PARENT_DIR=%PARENT_DIR:\= %
set LAST_WORD=
for %%i in (%PARENT_DIR%) do (
set LAST_WORD=%%i
if !count! LSS 6 (
set myDir=!myDir!\%%i
set /a count+=1
)
)
set myDir=!myDir:_= !
echo !myDir!
move "%hisDir%" "%myDir%"
andro299
 
Posts: 5
Joined: Tue Sep 11, 2018 5:14 pm

Re: Move folder/file one layer up

Postby andro299 » Sun Sep 23, 2018 8:41 pm

Also, if someone can optimise my code/make it work if the directory has empty spaces, please do so.
andro299
 
Posts: 5
Joined: Tue Sep 11, 2018 5:14 pm

Re: Move folder/file one layer up

Postby dude » Mon Sep 24, 2018 12:18 am

Code: Select all
@echo off
setlocal


::__________________________________
::
::   SETTINGS
::__________________________________
::
   set BASEPATH="E:\OverwatchVoiceLines\BASICS\Renamed\Ana"
   set FILETYPE=*.ogg
   :: Number of folders to move the file up.
   set MOVEUP=3


::__________________________________
::
::   INIT
::__________________________________
::
   set UPLEVEL=   
   setlocal enabledelayedexpansion
   for /L %%x in (1,1,%MOVEUP%) do set UPLEVEL=!UPLEVEL!\..
   endlocal & set UPLEVEL=%UPLEVEL%
   


::__________________________________
::
::   ACTION
::__________________________________
::
   
::   Get all .ogg files in basepath and process them one by one in the :PROCESS routine

   for /f "usebackq delims=" %%x in (`dir /s /b "%BASEPATH%"\%FILETYPE%`) DO call :PROCESS "%%x"
goto :EOF



:PROCESS

:: Simulation
   echo. FILE    : %1
   for %%a in ("%~1%UPLEVEL%") do    echo. MOVE TO : "%%~fa"
   for %%a in ("%~dp1") do    echo. REMOVE  : "%%~fa"
   for %%a in ("%~dp1\..") do    echo. REMOVE  : "%%~fa" (if empty)
   echo.


:: Execution
   rem Remeber: filename is already ""
   :: Move file %MOVEUP% levels up
   rem execute: move %1 "%~1%UPLEVEL%"
   :: Remove the now empty parent folder (even if %1 no longer exists)
   rem execute: rd "%~dp1"
   :: And remove the grandparent folder (if empty)
   rem execute: rd "%~dp1\.."

goto :EOF


dude
 

Re: Move folder/file one layer up

Postby andro299 » Mon Sep 24, 2018 3:02 pm

^This man is a genious. Thanks!
Works like a charm. It managed to keep all my files (my version lost one :cry: )
andro299
 
Posts: 5
Joined: Tue Sep 11, 2018 5:14 pm

Re: Move folder/file one layer up

Postby dude » Tue Sep 25, 2018 10:00 am

You're welcome :D

(
was just passing by and saw you taking some serious effort (respect!)
So I decided to register to help you out. Enjoy!
)
dude
 

Re: Move folder/file one layer up

Postby neurocrash » Wed Apr 21, 2021 4:47 am

I know this is an old post, but in case anyone is still notified of replies, I wanted to ask...

I pasted the code into a bat file, made the changes to my path and depth, and tried to run it. When I try to run this from the command line, I get the following error at this line:

---
The system cannot find the file specified.

for /f "usebackq delims=" %%x in (`dir /s /b "%BASEPATH%"\%FILETYPE%`) DO call :PROCESS "%%x"

in my case this is the echoed line:

for /F "usebackq delims=" %x in (`dir /s /b ""E:\Tempo\CUES with FLAC""\*.flac`) DO call :PROCESS "%x"
---

I'm guessing that I'm not running this code correctly. Am I not supposed to run this from the windows command line as a batch file? It doesn't appear to be Javascript for the BRU Javascript renaming function, so I'm not sure how to use it.

Thanks for your consideration
neurocrash
 
Posts: 5
Joined: Wed Apr 21, 2021 3:01 am

Re: Move folder/file one layer up

Postby neurocrash » Wed Apr 21, 2021 4:51 am

Sorry I forgot the code tags and it inserted emojis, but I can't edit the message:

Code: Select all
for /f "usebackq delims=" %%x in (`dir /s /b "%BASEPATH%"\%FILETYPE%`) DO call :PROCESS "%%x"

in my case this is the echoed line:

Code: Select all
for /F "usebackq delims=" %x in (`dir /s /b ""E:\Tempo\CUES with FLAC""\*.flac`) DO call :PROCESS "%x"
neurocrash
 
Posts: 5
Joined: Wed Apr 21, 2021 3:01 am

Re: Move folder/file one layer up

Postby andro299 » Wed Apr 21, 2021 9:12 am

The code he posted is indeed a bat file that should be executed in the cmd. I've just tested it myself and it still works for me.

For the following structure:
Code: Select all
test
    ?   movestuff.bat
    ?
    ????testtest
        ????badmap
                abc.txt

i set the params as such:
set BASEPATH="C:\Users\frogr\Desktop\test"
set FILETYPE=*.txt
:: Number of folders to move the file up.
set MOVEUP=3

which does produce a similar for loop
for /f "usebackq delims=" %x in (`dir /s /b ""C:\Users\frogr\Desktop\test""\*.txt`) DO call :PROCESS "%x"

The simulated operations are:
FILE : "C:\Users\frogr\Desktop\test\testtest\badmap\abc.txt"
MOVE TO : "C:\Users\frogr\Desktop\test"
REMOVE : "C:\Users\frogr\Desktop\test\testtest\badmap\"
REMOVE : "C:\Users\frogr\Desktop\test\testtest" (if empty)

Then after uncomenting the commands under ::Execution and running the file again, the final structure is as follows:
Code: Select all
test
        abc.txt
        movestuff.bat
andro299
 
Posts: 5
Joined: Tue Sep 11, 2018 5:14 pm

Re: Move folder/file one layer up

Postby neurocrash » Wed Apr 21, 2021 10:21 am

Hi,
Thank you very much for responding.

I think the problem is that my basepath folder has spaces in the name, and it gives me an error that way. After I renamed the basepath folder to something without spaces and adjusted the batch file accordingly, the error went away.

Thanks!
neurocrash
 
Posts: 5
Joined: Wed Apr 21, 2021 3:01 am

Re: Move folder/file one layer up

Postby Luuk » Fri Apr 23, 2021 1:08 am

In the BRU menu, first to put a checkmark in "Renaming Options, Advanced Options, Allow using '\' in Renaming Criteria".
Inside Filters(12) "Mask" is where you put matches like *.txt and then put checkmarks only in 'Files' and 'SubFolder'.
Inside Add(7) the "Prefix" is ..\..\..\ to move all files up three folders in their folder-tree.

But the posted batch would take much editing to make it safe, and to conduct like the description.
The only thing that Im guessing, is that maybe someone altered the batch, after it got posted?
Luuk
 
Posts: 325
Joined: Fri Feb 21, 2020 10:58 pm

Re: Move folder/file one layer up

Postby neurocrash » Fri Apr 23, 2021 7:56 am

Luuk wrote:In the BRU menu, first to put a checkmark in "Renaming Options, Advanced Options, Allow using '\' in Renaming Criteria".
Inside Filters(12) "Mask" is where you put matches like *.txt and then put checkmarks only in 'Files' and 'SubFolder'.
Inside Add(7) the "Prefix" is ..\..\..\ to move all files up three folders in their folder-tree.

But the posted batch would take much editing to make it safe, and to conduct like the description.
The only thing that Im guessing, is that maybe someone altered the batch, after it got posted?


I was wondering if it might be possible to including \, so that file/path names like *\foldername\* could be changed to *\* (not sure of the correct syntax, but I hope you get the idea. The objective in my case isn't to move things up a certain number of folder levels, but just to flatten folders of a certain name to their parent folders such as D:\Category\UnneededFolderName\File to D:\Category\File

I didn't see that menu option so I'll have to test it. Thanks for bringing this to my attention,

John
neurocrash
 
Posts: 5
Joined: Wed Apr 21, 2021 3:01 am

Re: Move folder/file one layer up

Postby Luuk » Fri Apr 23, 2021 8:17 pm

Im not understanding completely, but \ can only be added, not matched inside of the folder path.
Except that javascript does grant matching \ inside the folder path, so maybe its what your needing?
Like to only match the files in a parent folder called "BadFolderName", the Filters(12) "Condition" is like...
/.*\\BadFolderName\\$/.test(object('folder'))

The Filters(12) "Mask" does grant or forbid certain foldername or filename matches, but not pathname matches.
There is Copy/Move(13) "Path" to flatten the path, but Im thinking its not the automatic way that you are looking for?

Another way besides javascript, to only match files by their parent folder name....
First to put a counterfeit drive like X:\ in the top folder path, and press <enter> to remove all filenames from the list.
Then inside Filters(12) to put checkmarks only in "Files" and "Subfolder", then to put your file matches inside of "Mask".

Then to use something like Windows Explorer to find the BadFolderNames, maybe something like kind:folder BadFolderName.
So then, just drag the folders into the BRU list, so then all the matching filenames will present themselves in the list.
But if you change anything inside Filters(12) they will instantly disappear, so then having to drag your folders again.

Without javascript, Im thinking that dragging the folders with Copy/Move(13), is probably the best you can conduct them.
But stay away from the batch, because the way it looks right now, it will delete (overwrite) all existing same-named files.
It also would never conduct like the description says, because of using \.. instead of ..\ so never moving upward enough.

At first, Im going fix everything about the batch, but instead now Im just waiting to see if it gets altered again.
So Im copied the batch to verify this, because I thought it was illegal to edit the post, and this a good reason for it.
At some point, it had to be conducting properly, because everyone was satisfied, but its not safe right now.
Luuk
 
Posts: 325
Joined: Fri Feb 21, 2020 10:58 pm


Return to BRU Support