How to auto create folder based on filename and move the file into it's folder using .BAT - windows

I have already solved my question... What I haven't solved is how to do this if the .bat file is located in a parent folder and that it should work on all subfolders?
Right now, there's a limitation that it only create folders if the .bat file is located in the same folder as the files. It can't create folders if the files are inside a subfolder.
What I have is:
the filename of this .bat is :
organize.bat
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
How I do it right now:
I place the .bat file in a folder together with the files
When I click it, it will create folders with a name based on the files inside that folder
It will also move each files in those folders of the same name
What I need:
Place the .bat file in the main folder with many subfolders containing the files
Click it to perform the same tasks above
Apologies if my explanation is confusing... I hope it's still understandable.
Thank you in advance!

Your attempt is very close to working but beware the wrinkles of using a simple approach without checking each detail so, start here:-
#echo off & Title %~n0
REM I recommend using cd /d "%~dp0" to ensure you start from the known cmd file folder location not some system folder
cd /D "%~dp0"
REM add the /R switch to run through subdirs
for /R %%i in (*) do (
REM replace organize to %~n0 so as to aid renaming by other users
if not "%%~DPni" == "%~DPn0" (
REM to allow for nested paths we need a fuller DP location for N (check it works exactly as desired then remove the echos)
echo md "%%~DPni" && echo move "%%~i" "%%~DPni"
)
)
BEWARE files with double .dot.s such as cmd.exe.lnk so check those echo's first
md "C:\Users\me\Favorites\Links\cmd.exe"
move "C:\Users\me\Favorites\Links\cmd.exe.lnk" "C:\Users\me\Favorites\Links\cmd.exe"

Related

How to delete a specific sub-folder in a parent folder using a batch file?

I would like to create a batch file deleting or removing the folder C:\temp\root\students\type1, its subfolder and all the files.
The folders and files I do have are as follows:
C:\temp
C:\temp\root
C:\temp\root\students
C:\temp\root\tutors
C:\temp\root\students\type1
C:\temp\root\students\type2
C:\temp\root\tutors\type1
C:\temp\root\tutors\type2
C:\temp\root\students\type1\details.txt
C:\temp\root\students\type1\assignment1
C:\temp\root\students\type1\assignment1\results.txt
The folder C:\temp\root\students\type1\assignment1 is specified in the batch file.
I would like to go one folder/directory up and delete or remove C:\temp\root\students\type1 in my batch file (test.bat).
Please help me with this.
Use this:
rd /s /q C:\temp\root\students\type1
It will remove files and folders recursively, caution, without prompt. It's kind of a substitute for the good old deltree. Best.
EDIT: within your notepad, create your mygoodbatch.bat with this content:
md C:\temp
md C:\temp\root
md C:\temp\root\students
md C:\temp\root\tutors
md C:\temp\root\students\type1
md C:\temp\root\students\type2
md C:\temp\root\tutors\type1
md C:\temp\root\tutors\type2
md C:\temp\root\students\type1\details.txt
md C:\temp\root\students\type1\assignment1
md C:\temp\root\students\type1\assignment1\results.txt
rd /s /q C:\temp\root\students\type1
(md is the make dir dos command, so all the lines beginning with it are creating the folders - the last line is the one to del your level 4AA)
Save this batch on the root of your disk and then run it. I really wish this is what you want. What I yet didn't understand is: you are creating a directory structure and, at the same time, excluding level 4AA... is that it?
You can use a for loop to get the parent folder for a given folder:
set target_dir=C:\temp\root\students\type1.test\assignment1
for %%a in ("%target_dir%") do (
echo Removing %%~dpa%
rd /s/q %%~dpa%
)

Create subfolder from suffix of filename then move file

I have got several image files in one folder sharing the same filename but have a different suffix
i.e:
textures\texture_suffix.dds
textures\texture_suffix1.dds
...
textures\texture_suffix10.dds
Using a batch I want to create subfolders named Texture.Suffix, Texture.Suffix2 based on the suffix of the files under textures, then move the respective file into its subfolder and rename it 'texture.dds'.
Can this be done using batch file?
Thanks for any help.
cd textures
for /f %%i in ('dir /b /s') do (
md %%~ni
move %%i %%~ni\ )
pause
write these tags in a bat file and run the bat file in the same directory that includes textures directory

Files having same prefix to be moved to another directory

I have a files which doesn't have extension ending A_INI, A_FIF. And I need to write a code to search this files by user entering only A, if Exist I need to copy A_INI, A_FIF to another folder.
Example folder contains A_INI, A_FIF, A_LOG by prompting user enters file name as A.
I have to check files starting with A_* and if exist i have to move it to another folder.
How can this be achieved using batch script?
Not tested:
#echo off
setlocal
set "source_dir=C:\source"
set "destination_dir=C:\dest"
set /p "mask=Enter a pattern: "
pushd "%source_dir%"
for %%# in ("%mask%_*") do (
copy /y "%%~#" "%destination_dir%"
)
endlocal

How to Add prefix to all file in current folder and subfolder using batch file

I am trying to add prefix to all files in current folder and it's subfolders and i made the following batch file
setlocal enabledelayedexpansion
for %%j in (*) do (
set filename=%%j
rename !filename! [nilesh.uk.to]-!filename!
)
it working for current directory only but i want to add prefix to all files in current folder as well as subdirectory please help me to solve this problem
Run this from another folder, and the pushd is used otherwise the batch file will be renamed too. Change c:\mainfolder to your main folder name
#echo off
pushd "c:\mainfolder\"
for /r %%j in (*) do (
rename "%%j" "[nilesh.uk.to]-%%~nxj"
)
popd
using renamer, you can do it in one command like this:
$ renamer --regex --find '^' --replace 'prefix-' '**'

Batch file to create subfolder in other subfolders, but only at top level

I have a similar problem to this post, but I couldn't get the solution to work (I'm a total novice).
I have a set of folders on a network drive, each folder named after a manager (e.g. \sfnetfile\DEPT\HR_Manager\Manager, Test and \sfnetfile\DEPT\HR_Manager\Manager, Test 2). I am trying to get a folder/subfolder combo created in each manager folder of 2013\Variable. In some cases managers already have a 2013 subfolder, but not the Variable sub-subfolder (excuse the silly terminology).
I also want to prevent the batch file from creating the 2013\Variable folder in existing other subfolders within each manager folders (so I don't want it, for example, creating another level down: \sfnetfile\DEPT\HR_Manager\Manager, Test 2\2012\2013\Variable.
Here is my code:
FOR /d %A IN (\sfnetfile\DEPT\HR_Manager*) DO mkdir "%A"\2013\Variable
Something like this should do:
#echo off
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
assuming that sfnetfile is the name of a remote server and DEPT is a shared folder on that server. If sfnetfile is a top level folder on a mapped network drive, change that to:
#echo off
for /d %%d in (X:\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
where X: is the drive letter of the network drive.
Edit: As #dbenham correctly mentions, redirecting error output (to suppress error messages for existing leaf folders) will also suppress any other error message. A more sophisticated approach would check if the folder doesn't already exist and only then create it:
#echo off
setlocal EnableDelayedExpansion
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do (
set "folder=%%~d\2013\Variable"
if exist "!folder!" (
if not exist "!folder!\" echo "!folder!" exists as a file! 1>&2
) else (
mkdir "!folder!"
)
)

Resources