Category filter
Script to Create Files and Folders on Windows devices
It is simple to create a file or folder on a device. However, creating files and folders on a large number of devices in a specified location becomes a time-consuming task. Hence, you can simplify this process on Windows devices by deploying custom scripts from Hexnode.
Create Files
Batch Script
|
1 2 |
@echo off echo "Hello">C:\filename.txt |
Create a file after checking whether it already exists
|
1 2 3 4 5 6 7 8 |
@echo off Cd “Location where the file needs to be created” If exist filename.ext ( Echo “A file with this name already exists” )else ( Type nul>filename.ext Echo “File has been created” ) |
Replace ‘filename‘ with a file name of your choice and make sure the file extension is the same throughout the script. Provide the path where you want the file to be created.
PowerShell Script
|
1 |
New-Item D:\temp\test\pc.txt |
Create a file after checking whether it already exists
|
1 2 3 4 5 6 7 8 9 10 |
#create array of text files $files=Get-ChildItem C:\data\*.txt | select -expand fullname #check if file exists inside the array $files -match "pc.txt" if($files -match "pc.txt") { Write-Host “File Exists” } else { New-Item C:\data\pc.txt } |
Replace ‘pc‘ with a file name of your choice and make sure the file extension is the same throughout the script. Provide the path where you want the file to be created.
Create Folders
Batch Script
|
1 |
md C:\test |
PowerShell Script
|
1 |
New-Item -Path "C:\" -Name "test" -ItemType "directory" |
Replace ‘test’ with a folder name of your choice and provide the path where you want the folder to be created.