File name and extension lost during move -cmd - windows

This little code moves folders into subfolders ( by the way I found this here on stackowerflow. thanks :) ). I added some other functions and it works nicely except one part. See below the code, the problematic part is the last part here.
I wanna search for .txt files in the folders and move them up right under the parent directory (this part is working). But unfortunately the files lost their extensions and also their name changes to the name of the parent dir. You can see it on the attached image (I copied not moved the file there):
#echo on
setlocal EnableDelayedExpansion
set "ROOT_FOLDER=C:\Users\xy\Desktop\folder"
:: For each folder in root folder
cd "%ROOT_FOLDER%"
for /D %%a in (*) do (
cd "%%a"
:: Move all existent folders into "XY" folder
for /F "delims=" %%b in ('dir /B /A:D') do (
md XY 2> NUL
move "%%b" "XY\%%b"
)
:: Move all existent files into "XY" folder
md XY 2> NUL
move *.* XY
:: Move txt files under parent folder
for /R "XY" %%m in (*.txt) do move "\\?\%%m" "%%a"
cd ..
)
pause
Can you help what I'm messing up here?

Ok, meanwhile I figured it finally.
The %%a was the problem. The program needs the full path to move the file correctly:
for /R "XY" %%m in (*.txt) do move "\\?\%%m" "%ROOT_FOLDER%\%%a"

Related

Moving files from Subfolders into root directory, but not copying them to the next directory when ran again

I have a folder that contains subfolders with MP4 files. I'm trying to write a script that will move the MP4 files out of the subfolders into the root folder when ran. The batch file I wrote is working, but when the batch script runs again for new subfolders, the MP4 files that were already copied to the root folder, get moved up another level in the file structure. For example:
C:\MainRoot\Root\Subfolder\media.mp4
When script is ran, 'media.mp4' gets moved up to C:\Root\media.mp4 as desired.
But since I need the script to run on a scheduled task. The next time the script runs I get the following:
C:\MainRoot\media.mp4
Instead of just the MP4 file staying in C:\MainRoot\Root.
Here's my batch file so far to copy the mp4 files:
set root_folder=C:\MainRoot\Root
for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".mp4" ') do (
move /y "%%G" "%%~dpG..\%%~nxG"
)
What do I need to modify so that once moved, the MP4 files will stay in place?
Any help would be greatly appreciated!
Since all your source files seem to be at a certain directory level, a for /D loop could be wrapped around your for /F loop, which parses the output of a non-recursive dir command line (no /S):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=C:\MainRoot\Root"
set "_PATTERN=*.mp4"
rem // Loop through sub-directories:
for /D %%D in ("%_ROOT%\*") do (
rem // Loop through matching files:
for /F "eol=| delims=" %%F in ('dir /B "%%~fD\%_PATTERN%"') do (
rem // Avoid overwriting destination file:
if not exist "%_ROOT%\%%~nxF" (
rem // Move matching file one level up:
move /Y "%%~fD\%%~nxF" "%_ROOT%\%%~nxF"
)
)
)
endlocal
exit /B
If you are happy to overwrite as in your provided example then something as simple as this may suit your purpose:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /R %%G In (*.mp4) Do If /I NOT "%~dpG"=="%root_folder%\" Move "%%G">Nul 2>&1
If the files are only one folder deep you may prefer this:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /D %%G In (*) Do Move "%%G\*.mp4">Nul 2>&1

how to move folders with a loop over folders (in batch)?

Situation:
I try to move files inside a loop in shell but my code is not working.
for /D %%F in (*) do (
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
)
)
After hours of testing it, I realized it's because %%F is pointing to the folder, hence the file cannot be moved.
Bad solution:
The way I made it work and confirmed my suspicions is by saving the value of %%F in another variable and using that variable on next turn to move the file. Note, what is following needs an initialisation of %precedentFile% for the first turn.
for /D %%F in (*) do (
move /y "%precedentFile%" "%directoryToPutFilesIn%"
if "%%F" NEQ "%directoryToPutFilesIn%" (
move /y "%%F" "%directoryToPutFilesIn%"
set precedentFile=%%F
)
Problem:
This solution is not practical and feels wrongs. Is there a way to adapt my current code to do it, or simply another way?
Try below code to move files from one folder to another in batch script:
for /f %%a in ('dir /a:-D /b') do move /Y "%%~fa" "%directoryToPutFilesIn%"
Explanation :
dir /a:-D /b : This command will list all files in the directory
move /Y "%%~fa" "%directoryToPutFilesIn%" : This will move all files in the directory where this command is executed to the destination you have mentioned.
%%~fa : This command will get full qualified path of the file with it's name.
Try Below code Move directories :
Below command will move directories in the Path where this command is executed to the destination provided. In this that will be H:\ Drive, Change it accordingly
for /D %%b in (*) do move /Y "%%~fb" "H:\"

Batch file to move files and folders in a folder to another folder

I am using the below but it is only moving the files and not the folders in the source folder.Is there anything I can add?
move "C:\source\*" "C:\destination\"
This is another way: test it on sample folders first.
robocopy "C:\source" "C:\destination" /move /s
EDIT: Robocopy copies the files and then deletes the original, so will take a long time for large files, even if the source and target locations are on the same hard drive.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\source"
set "target=c:\destination"
(if not exist "%target%\" md "%target%" ) && (
pushd "%source%" && (
for /f "delims=" %%a in ('dir /a /b *') do move "%%a" "%target%\"
popd
)
)
Ensure that target folder exist, then, if source folder is accesible, change active directory to the source folder, and for each element inside it, execute a move operation to the target folder

Run bat cmd in every sub-folder fail

I need to run a bat file to rename the photo in each sub-folder as the sub-folder name and copy all the photos to a new folder. Each sub-folder contains only one photo.
I would like to write a batch file to do this task as there are many sub-folders, however, I can only successfully copy the photo in the last sub-folder.
The sub-folders are naming by number sequence starting from "00000001".
I found that the photos are renamed as the same thus only one photo can be copied.
Here's my code:
md "D:\photo"
for /r %%d in (.) do (cd %%d
for /r %%* in (.) do (set CurrDirName=%%~n*
echo %CurrDirName%
ren "*.jpg" "%CurrDirName%.jpg"
copy *.jpg "D:\photo"))
Please advice so that I could modify my code and do what I would like to do successfully, thanks a lot!
EDIT with more details
In cmd line, for example the program are running in the sub-foler "00000127"
set CurrDirName=00000127
but echo the %CurrDirName%, shows 00000128 which is the last sub-folder and every sub-folder return 00000128 but not the CurrDirName
Try this & remove the echo if the output is ok.
#echo off &setlocal
set "destination=d:\photo"
md "%destination%" 2>nul
for /d /r %%d in (*.*) do (
pushd "%%d"
for %%i in (*.*) do echo copy "%%~i" "%destination%\%%~nd.jpg"
popd
)
endlocal
There must be only one photo in each subfolder!

Move Files to Folders with same name

I have 1000 files with the suffix -PRO1 and -PPR2 (1000 each) so I have 1000 folders with the same names but without the suffix...
For example I have a folder called Abstract_Colorful and I have the files Abstract_Colorful-PRO1 and Abstract_Colorful-PPR2 and so on...
I want to make a batch to be able to move all files automatically, I have this code (from another post)
#echo off
setlocal enabledelayedexpansion
pushd "C:\Folders\"
for %%a in (*) do (
set fldr=%%~na
set fldr=!fldr:~0,4!
md "!fldr!"
move "%%a" "!fldr!"
)
popd
pause
exit
but what it does is that if the file has more than 4 characters it creates a folder with the first 4 chars... What I want to do is that the batch recognizes the filename and stops at the - and moves to the folder...
Thanks for your time :)
#echo off
pushd "C:\Folders"
rem Process all files in this folder separating the names at "-"
for /F "tokens=1* delims=-" %%a in ('dir /B *.*') do (
rem At this point %%a have the name before the "-" and %%b the rest after "-"
rem Create the folder, if not exists
if not exist "%%a" md "%%a"
rem Move the file there
move "%%a-%%b" "%%a"
)
popd
A very simple way to do this:
use the search bar in the Windows gui to view all files with the specific suffix (ie. -PRO1)
select all in the GUI
use the Move option in the window menu to create your new folder in the right directory and move all selected files into it

Resources