Windows CMD: Create Folders within multiple SubFolders - cmd

Ive got a Folder called "Projects" which contains about 50 SubFolders [Project 1, Project 2, etc...].
Within each SubFolder [Project 1, Project 2, etc...] I would like to create a new Folder [current year] automatically.
In the end it should look a bit like that:
C:⁄Projects⁄Project 1/2021
C:⁄Projects⁄Project 2/2021
C:⁄Projects⁄Project 3/2021
...
C:⁄Projects⁄Project 50/2021
Ive already tried mkdir but I couldn't figure out how to tell Windows cmd.exe to create the folder 2021 within those multiple subfolders in "Projects" without addressing every single directory...
I hope some of u got an idea!

Use a for /d loop to loop over the subfolders:
for /d %a in ("C:\Projects\*") do md "%a\2021"
If you want to use it in a batchfile, use %%a instead of %a.
See for /? for more information.

Related

Windows - bulk symbolic link files and folders resursively

I have a folder (say Folder A) which contains many files and folders (those folders also have files inside it).
I want to create symbolic links with each content of the parent folder (Folder A) recursively within a new folder (Say Folder B).
I've been searching for hours and there are many solutions but none of them are working for me. Maybe I am missing something (I don't know batch file scripting). In Linux I could have done that. But I need this in Windows.
In Windows I could have done, mklink /J "D:\Folder_B" "D:\Folder_A", but this will junction the whole folder. I need to keep some files only specific to Folder B.
Can someone help me with this?
I solved it, this might not be a proper batch file but this is what I did and works!!
#ECHO OFF
set source_dir="D:\Folder_A\"
set target_dir="D:\Folder_B\"
FOR /d %%X in (%source_dir%*) DO (
mklink /J %target_dir%%%~nX %%X
)
FOR %%I in (%source_dir%*.*) DO (
mklink %target_dir%%%~nxI %%I
)

Search multiple files in Windows, copy to new folder

I'm running Windows and I have a massive group of .pdf files (several thousand) all stored in one large directory, and then all variously organized into a variety of subdirectories within that main directory.
e.g.:
K:\OriginalDirectory [main directory]
K:\OriginalDirectory\2010 [subdirectory 1]
K:\OriginalDirectory\2011 [subdirectory 2]
K:\OriginalDirectory\2013 [subdirectory 3]
My problem: I need to copy-and-paste about 1900 of these files into a new directory. The Windows "find" function won't let me do this, as trying to use the "or" operator to combine 1900 unique file names exceeds the 255 character limit for "find."
So I tried the following (based on another StackOverflow answer), using the command line:
C:\OriginalDirectory>for %I in (doc1.pdf doc2.pdf doc3.pdf) do copy %I C:\SomeOtherDirectory
This works, but doesn't search the subdirectories. Also, it requires me to type out all 1900 file names, which isn't ideal.
Is there a way for me to complete this task using just the command line that searches all subdirectories within my main directory AND doesn't require me to type out 1900 file names?
You can use a batch script which would do the work.
Found on: Windows batch copy files from subfolders to one folder
Adjusted for you it should be something like:
pushd C:\OriginalDirectory
for /r %%a in (*.pdf) do (
COPY "%%a" "C:\SomeOtherDirectory\%%~nxa"
)
popd
edit cause of comment
Copy only those files which are in a list: (Again not sure about the type "C:/my..." because i am currently not able to test it but should work in such a way)
pushd C:\OriginalDirectory
for /r %%a in (type "C:/myFileList.txt") do (
COPY "%%a" "C:\SomeOtherDirectory\%%~nxa"
)
popd

Moving files with .bat script on windows 10

Recently I wanted to make .bat script that would move .avi files from subfolders in specified directory to another directory.
e.g.
H:\MAINDIRECTORY\dir1\avi1.avi
H:\MAINDIRECTORY\dir2\avi2.avi
H:\MAINDIRECTORY\dir3\avi3.avi
....
To one directory called e.g.
H:\Movies
I've made script which looks something like this
#echo off
move H:\Pobrane\*\*.avi H:\Filmy
But when I'm tryin to execute it I got something like this:
the file name, directory name or volume lable syntax is incorrect
Please help me find a way out from this situation.
As move does not support wild cards you can try with for /r
#echo off
for /r "H:\Pobrane\" %%# in (*.avi) do (
move /y "%%~f#" "H:\Filmy"
)

Windows CMD or BATCH file that "flags" empty folders (and folders that only contain empty folders)

Most companies I have worked for start new projects with a templated folder structure. Windows will automatically flag empty folders (the icon shown in the file explorer is that of an empty folder) however, folders whose subfolders are also empty will not be flagged (their icon shows a folder containing files). This can create confusion and lead to mistakes during the early stages of a project as it would appear at a glance that some folders have had their content added when they really only contain empty subfolders.
So my question is:
How can I iterate over folders with empty subfolders and "flag" them via the use of a Batch file.
The .bat file would need to search through subfolders to determine if a folder has any real content. If the folder does not have real content then the .bat file would need to flag it (flagging could be done with a change of its icon or filename). This would make it much easier to navigate new projects with large templated folder structures.
I don't need a completed file, I would just like to know how it could be achieved. However, any tips or suggestions on how to achieve this functionality would be more than welcome!
*Edit
Just to clarify I will show an example:
If I create an empty folder called 'Project' it will display with the Empty Folder icon. As shown Below:
Now I will add a new folder to my project folder called 'I am Empty':
The folder 'Project' no longer displays with the Empty Folder icon. It now uses the icon that shows it with content. As shown Below:
What I want is a .bat file that will parse the contents of the 'Project' folder and determine that it only contains the 'I am Empty' folder, which is empty and "flag" that folder (to flag it we could change the icon of the 'Project' folder back to the Empty Folder icon, change the name or "gray it out"). As shown Below:
cd example
for /d %%i in (*) do #dir /b /s /a-d "%%i">nul 2>&1|| #echo "%%i" has no files
give for an additional /r if you want to check subfolders too.
The trick is to list files only (/a-d) in the folder and all subfolders (/s) and if this fails (||) (because there are no files), do something with this folder (just echo here, but rd /s /q or ren is also possible)
Building on the answer given by Stephan and looking at other related stack exchange posts I pieced this solution together and it works well for my needs:
#ECHO OFF
PUSHD "%~dp0"
FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO (
#dir /s /a-d-s "%%F">nul 2>&1|| ATTRIB +H "%%F"
#dir /s /a-d-s "%%F">nul 2>&1&& ATTRIB -H "%%F"
)
POPD
EXIT
I opted for a solution that did not modify file names. While testing that approach I realized it doesn't create the best user experience.
Instead, running this batch file hides the folders that are effectively empty and if your folder settings allow you to see hidden folders then they appear faded out. It also re-shows hidden files that have new content since the last time the batch file was ran.
For those of you coming to this solution who, like myself, are relatively inexperienced with batch scripting. I will explain what the code does and why (as I understand it).
I don't want to change the batch file for each implementation so I call PUSHD "%~dp0 to set the active directory to the folder containing the batch file (this lets me include the batch file in the folder-structure template, which is copied and pasted for each project)
Since I decided to use the hidden attribute for folder flagging, I needed to modify the FOR loop . FOR loops typically ignore hidden files which becomes troublesome if you need show a file that was previously hidden because it has new content. Running FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO ()
allows the batch file to loop through all files mainly because of the /f attribute, but check out
this post about looping through hidden folders for more information.
Inside the for loop I am pretty much doing what Stephen suggested in his answer with the added logic to remove the hidden attribute on folders that no longer need it.
The only thing this batch file is lacking, is the ability to auto update on folder modifications or to auto-run on folder open (I hear this might be possible with an .ini file?) however, for my needs it will suffice to rerun the batch file, manually, after making changes to the folder.
Batch scripting is way outside of my comfort zone as far as scripting languages go so please forgive and correct me if I have made any mistakes or if there is a more reliable way to do what I need.

Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder

Tried my best searching for a solution but close to my need was this example which did not work. Bash: Moving multiple files into subfolders
I am not a programmer so unable to create the batch file myself for Windows 7. Any help will be appreciated.
Needed code for a batch file that does the following:-
Searches the folder for all files that have "_F1" in the file name
Creates a subfolder named as "F1" where this file is located
Move all the files searched in step 1 to the folder "F1" created in step 2
Ideally, the batch file should execute from parent folder and should complete the 3 steps in all subfolders at least till 3 levels down the parent folder.
Thanks in Advance for any help.
I tried and came up with this. Works, but is very raw. Needs to be run manually from inside of each folder (100's of them)
MKDIR F1
MKDIR F2
DO 500
move *_F1*.* F1
move *_F2*.* F2
ENDDO
Try like this :
#echo off
for /f "delims=" %%a in ('dir /s/b/a-d *.* ^| find /i "_F1"') do (
if not exist "%%~dpaF1" md "%%~dpaF1"
move "%%~fa" "%%~dpaF1")

Resources