Populating empty folders recursively with Windows - windows

I have a huge series of folders, named 'A' through 'Z' such that each folder has subfolders in the same form.
For example, I could have a directory at this path: .\A\D\E
I want to populate each of these folders with an empty file (so I can commit it to a git repository if anyone's curious).
Windows doesn't have an equivalent of Linux's touch, but I have the GnuWin32 toolset installed so I can, in fact, use touch in my Windows environment.
I've started by creating a batch file with the following:
FOR /D /r do touch empty
But when I run it, the folders aren't populated. I don't get any errors either:
C:\sandbox>FOR /D /r do touch empty
C:\sandbox>
Does anybody see anything glaringly wrong about the line of batch script above? Is there anything else I can try short of using additional non-Windows commands?

You just need to get the syntax of the FOR command right:
for /r %f in (.) do touch %f\empty

Related

Update timestamp of a directory on Windows

There is more or less well-known command line way to update the modification time of a file on Windows (described at Update file or folder Date Modified, for example):
copy /b somePath\fileName+,, somePath\
According to my experience it does for a file, but does not for a directory (tested on WinXP - the command did not fail, but the directory modification time was not updated).
I tried to adjust it for a directory using such a trick that we can "point" to the directory using a special "NUL" filename on Windows. I tried two ways to do that, but they don't work as well:
copy /b somePath\fileName\NUL+,, somePath\filename\
copy /b somePath\fileName\NUL+,, somePath\
Could anyone explain me why it does not work or what I am doing wrong?
It doesn't make any changes to the directory because the filename nul is not stored in the directory. Since the directory is not changed, its modification time doesn't change. You can do this instead:
type nul > somePath\fileName\SomeFileThatDoesNotExist.tmp && del somePath\fileName\SomeFileThatDoesNotExist.tmp

How do you match an exact file extension under Windows/command line/file open dialog?

I am trying to get a directory listing of only files with a given extension. At first blush this seems to be a simple thing to do, however check out this simple example:
C:\CODE\metcal>dir /b *.exe
metcal.exe
metcal.exe1
Notice that this returns metcal.**exe** and metcal.**exe1** as matches.
With python files a similar thing happens:
C:\CODE\metcal>dir /b *.py
metcal.py
metcal.pyc
Notice again Windows has determined that *.py takes anything that starts with *.py so it captures the .pyc files as well.
Is there a way to get only the extensions that match exactly? In the above python files example I would like the following to occur (obviously with the correct syntax substituted for *.py)
C:\CODE\metcal>dir /b *.py
metcal.py
As a note the matching under Windows not as simple as it seems.
*.exe matches foo.exe, foo.exe1, foo.exeabcde but not foo.exe.bak
There are other questions on SO that are similar that are related to long/short file names. The *.py and *.pyc example here should not introduce name mangling machinery.
**I have experimented on XP and Win7 machines and this behavior is not consistent at the cmd Prompt and file open dialogs. This inconsistant behavior makes me suspect this problem is related to settings of somekind. **
It's because windows wildcards on extensions check both long and short names as explained in this answer:
https://superuser.com/questions/238900/winxp-dir-command-3-and-4-char-extensions-are-the-same#238930
Solution there is to disable 8.3 names creation and then striping them on ntfs volumes which will also improve performance.
Microsoft Docs: Fsutil 8dot3name
Remarks:
Permanently removing 8dot3 file names and not modifying registry keys that point to the 8dot3 file names may lead to unexpected application failures, including the inability to uninstall an application. It is recommended you first back up your directory or volume before you attempt to remove 8dot3 file names.
So if you want to get only those extensions (.py and .pyc), you should try like this way :
#echo off
dir /b *.py*
pause
You can use the Unix ls command from the Windows Subsystem for Linux to do this, assuming you have WSL installed. It's freely available from Microsoft. From your Windows command prompt, type wsl followed by the ls command to list the files you want.
So, for your example, wsl ls metcal.py returns only metcal.py. To get the same results as you're seeing with dir, use wsl ls metcal.py*.

cmd line prompt for navigating to only folder in current folder. Windows

Suppose I have a folder called Documents, with only one folder inside, called Projects, as illustrated incredibly below...
Documents
Projects
If I'm in Documents, is there a cmd line prompt which will take me into the only available folder, in this case Projects?
So instead of using
cd Projects
I'm looking for
cd 'only available folder'
Is there a cmd for this?
In response to an answer which was deleted: I'm not looking for a list of folder contents. I'm looking to navigate directly into the only available folder.
I suppose one option is:
cd 'Tab button'
There's not really a simple command for this. Like you suggested, the best solution, and what I would do, is cdspaceTab. Strictly speaking, there is a command that satisfies your requirements, but it's not exactly easy under the fingers.
for /d %I in (*) do cd "%I"
which would loop through all directories in the current directory (ostensibly, only one), then cd to it.

Any reason to split up "cd" commands in Windows?

I'm cleaning up some old Windows batch files at work, and keep seeing lines like this:
D:
cd\
cd some\other\path\
is there any reason (compatibility with command.com maybe?) to not just write this as
cd /d d:\some\other\path\
cd doesn't actually change the working drive; it only changes the working directory for that drive. That's why it's broken up that way.
An example might help:
C:\users\david>cd D:\some\path
C:\users\david>
Note the drive hasn't changed.
C:\users\david>D:
D:\some\path>
Now that D: is the "working disk", the working directory is changed to what you specified previously.
As you point out, cd /d will do both. [UPDATE: I must have missed the /d when reading your original post :( -- sorry]
I believe you're correct -- there's no "good" reason not to use the one-liner -- other than compatibility with COMMAND.COM (which I'm pretty sure doesn't support the /d switch). For this reason, I always create my script files with a .cmd extension if they depend on features not supported in COMMAND.COM.
A better alternative IMHO is to use pushd which behaves like cd /d -- and also gives you the ability to go back to wherever you were before (via popd). You can even pushd to a UNC path (\\server\share) and Windows will create a temporary drive letter for you. (Although I only found that feature this morning, and I'm running Win 7 Pro, so I'm not sure if it's available on older versions and/or Home editions.)

What is equivalent to Linux mkdir -p in Windows?

In Linux, mkdir -p creates a folder tree.
What is the equivalent option in Windows to create a folder tree? Is there any?
In Windows, mkdir creates directory trees by default.
mkdir a\b\c
The Windows mkdir does it automatically if command extensions are enabled. They are on just about every box I've ever used but, if they're not, you can create your own script to do it:
#echo off
setlocal enableextensions
md %1
endlocal
Expanding:
Command extensions are an added feature of cmd.exe which allows you to do so much more (at the cost of a little compatibility with earlier incarnations of the batch language).
Windows XP cmd.exe should have these extensions enabled by default but you can configure your box so that they're disabled by default (using "cmd /e:off" as the default processor). If you do that and want to use the extensions, your cmd files must have a setlocal to turn them back on.
The script above could be called md2.cmd and then you would be guaranteed to be able to create multiple directory levels with "md2 a\b\c" without having to worry whether the extensions were enabled.
Almost every one of the cmd scripts I write begins with:
setlocal enableextensions enabledelayedexpansion
to ensure I get as close as possible to the behavior of my beloved bash :-)
For a strange reason when I attempted to create a directory with the following method;
mkdir src/main/java/main/resources
it didn't work, I had to surround the path in double quotes, as shown below;
mkdir "src/main/java/main/resources"
Additionally, unix allows for this;
mkdir -p src/main/java src/main/resources
where two branches will be created as shown below, the equivalent to that on windows is;
mkdir "src/java/resources" "src/main/resources"
src
-----java
-------resources
-----main
-------resources
I hope this helps!
xox
If you want to use forward slashes, just give the directory structure you want within double quotes.
mkdir "org/frame/bu/fed/config"
mkdir by default makes all intermediate directories. Just ensure that you use '\' as the separator.
I just try to create multiple folders on today and it is working!
mkdir "templates" "static/css" "static/js"

Resources