Cs.console Notes
Contents
Windows
Command Prompt
Creating Files
To create a file in Windows, you can use several methods:
-
Using Command Prompt: Open Command Prompt and use the
echo
command to create a file. For example:echo Hello, world! > myfile.txt
This will create a file named
myfile.txt
with the content “Hello, world!”. -
Using Notepad: Open Notepad, write the content you want in the file, then go to File > Save As, choose the location and filename, and click Save.
-
Using PowerShell: You can use PowerShell to create files. For example:
Set-Content -Path "myfile.txt" -Value "Hello, world!"
-
Using File Explorer: You can also create a file by right-clicking in the File Explorer, selecting New > Text Document, and then renaming the file as needed.
Scripts
Creating Scripts
To create a batch script (a file with a .bat
extension) using the Command Line Interface (CLI) in Windows, you can use any text editor like Notepad or you can use the command prompt itself. Here’s how to create a batch script using the command prompt:
- Open Command Prompt.
- Navigate to the directory where you want to create the batch script using the
cd
command (e.g.,cd path\to\directory
). - Type
copy con filename.bat
and press Enter. Replacefilename
with the name you want for your batch script. - Type your batch script commands.
- Press
Ctrl + Z
, then Enter to save the file and exit.
For example, to create a batch script that displays “Hello, World!” when executed:
copy con hello.bat
echo Hello, World!
^Z
This will create a batch script named hello.bat
in the current directory, containing the single line echo Hello, World!
.
After creating the batch script, you can execute it by simply typing its name in the command prompt and pressing Enter (e.g., hello.bat
).
CLI
EXAMPLE: removing duplicate mp3
files (first echoing):
FOR %f IN ("C:\Directory\SubDirectory\*(?).mp3") DO echo "%f"
This Windows command is a for
loop that iterates over files matching a specific pattern in a given directory and its subdirectories:
-
FOR %f IN ("C:\Directory\SubDirectory\*(?).mp3") DO
: This is the beginning of thefor
loop. It sets up a loop that iterates over files that match the specified pattern.FOR %f
: This sets up a loop variable%f
that represents each file matched by the pattern.IN ("C:\Directory\SubDirectory\*(?).mp3")
: This specifies the set of files to iterate over. The pattern"C:\Directory\SubDirectory\*(?).mp3"
is enclosed in double quotes and represents the path to a directory (C:\Directory\SubDirectory
) and a wildcard pattern for files (*(?).mp3
). The*
represents any sequence of characters,?
represents any single character, and.mp3
specifies that the files should have a.mp3
extension. This will be replaced bydel "%f"
.
-
echo "%f"
: This is the command executed for each file matched by the pattern. It echoes (prints) the name of the file enclosed in double quotes ("
).%f
is replaced with the name of each matched file during each iteration of the loop.
- Note that a batch script will interpret the
"%f"
, so"%%f"
must be used.