Using the matched wildcards in cmd - cmd

Can we access the matched character in wildcards?
I have some files in my current directory name as Labx.pdsprj and I want to copy them to a subfolder in the same directory based on their file names such that "Lab3.pdsprj" goes to the subfolder ".//subfolder_name//lab3" as ".//subfolder_name//lab3//Lab3.pdsprj"
I was trying something like
copy Lab?.pdsprj .//subfolder_name//lab(something here to access the matched number)
Its not necessary to use the above command only, any help is appreciated.

This may seem excessive if your requirement is not to have a case difference in the filename "Lab*" and the subdirectory "lab" as indicated by #dave_thompson_085. Although, that solution presumes that the "lab*" subdirectory already exists. I could be changed to do this.
FOR %%A IN (Lab?.pdsprj) DO (
MKDIR "%%~nA"
COPY %%A .\subfolder\%%~nA\%%~nxA
)
If there must be a case difference, this could be used. This will run on a windows command-line batch-file running under cmd. If you are on a supported Windows system, PowerShell was installed with it on the system.
powershell -NoLogo -NoProfile -File "C:\src\t\Do-CopyPdsprjFiles.ps1"
=== C:\src\t\Do-CopyPdsprjFiles.ps1
Get-ChildItem -File -Filter 'Lab*.pdsprj' |
ForEach-Object {
if ($_.Name -match 'Lab(\d+).pdsprj') {
$LabNumber = $Matches[1]
$LabDir = Join-Path -Path '.' -ChildPath "lab$($LabNumber)"
if (-not (Test-Path -Path $LabDir)) { mkdir $Labdir | Out-Null }
Copy-Item -Path $_.FullName -Destination $LabDir
}
}

Related

Need script to move folders based on certain file types in the folder

I have a scenario where I need to search folders and sub folders for certain file types. if those types are found then the whole root folder with all files and sub folders needs to be moved.
Source c:\testsource
example: search for *.exe files
the following file is found
c:\testsource\folder1\subfolder1\testfile.exe
then folder1 with all files and subfolders needs to be moved to c:\testdestination
I first tried this with a batch file and was able to move all files in subfolder1 but not any of the other files or directory structure. I then started working on it in powershell but had similar results. I think what I need is to search, and if found capture the folder path, then move the folder, but not sure how to do that.
Batch file I created:
for /r "C:\TestSource" %i in (*.exe)do move "%~dpi\*" "C:\TestDestination\"
Powershell Script
$Source = "C:\testsource"
$Dest = "C:\testdestination"
Get-ChildItem -Recurse -Path $Source | Where {$_.fullname -Match '.*\.exe'} | Move-Item -Destination $Dest
Any help here would be much appreciated
You will need a loop to check for each direct subfolder of the source folder if it has the desired file in one of its subfolders. ... something like this:
$Source = 'C:\testsource'
$Dest = 'C:\testdestination'
Get-ChildItem -Path $Source -Directory |
ForEach-Object{
If (Get-ChildItem -Path $_.FullName -Recurse -File -Filter '*.exe'){
Move-Item -Path $_.FullName -Destination $Dest -Recurse
}
}

CMD Dir : i can use * instead subfolder

This is my tree:
f:\mega\user1\rubbish\
f:\mega\user2\rubbish\
f:\mega\user3\rubbish\
.....
f:\mega\usern\rubbish\
I would like (ONLY) list all files inside the various "rubbish" folders.
I tried without success this command:
DIR F:\mega\*\rubbish\
Any suggestions?
Try this from a cmd.exe prompt.
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Directory -Path 'F:\mega\*\rubbish'"
Of course, it is easier in a PowerShell console or .ps1 file script.
Get-ChildItem -Directory -Path 'F:\mega\*\rubbish'
Or, possibly this to limit the names to 'user*' subdirectories.
Get-ChildItem -Directory -Path 'F:\mega\user*\rubbish'
To get only the fully qualified path to the files...
(Get-ChildItem -Directory -Path 'F:\mega\user*\rubbish').FullName
Wildcards can only be used in the last element of a path but not somewhere else.
You could however use a for /D loop to resolve the sub-directories:
for /D %I in ("F:\mega\user*") do #dir "%~I\rubbish"
To use that code in a batch file do not forget to double the %-signs:
for /D %%I in ("F:\mega\user*") do #dir "%%~I\rubbish"

Execute PS from bat for Intune Win32

I am so confused and actually also not so familiar with PS and bat after my troubles with them.
I want to set the Lockscreen in Windows 10 with Intune through an IntuneWin file (WIN32 application).
I have a folder with the image, which I want to set, a copy.bat which should copy the image in the directory and also execute the PS file for setting the login image and a del.bat for deleting the image.
copy.bat
md %AllUsersProfile%\sz
copy /Y Wallpaper.jpg %AllUsersProfile%\sz
powershell -ExecutionPolicy Bypass -File Set-Lockscreen.ps1 -verb RunAs
del.bat
del /Y %AllUsersProfile%\sz\Wallpaper.jpg
Set-Lockscreen.ps1
$RegKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationLS"
$LockScreenPath = "LockScreenImagePath"
$LockScreenStatus = "LockScreenImageStatus"
$LockScreenUrl = "LockScreenImageUrl"
$StatusValue = "1"
$path = "C:\ProgramData\Elinvar"
$LockScreenImageValue = "C:\ProgramData\sz\Wallpaper.jpg"
sIf ((Test-Path -Path $path) -eq $false)
{
New-Item -Path $path -ItemType directory
}
if (!(Test-Path $RegKeyPath))
{
Write-Host "Creating registry path $($RegKeyPath)."
New-Item -Path $RegKeyPath -Force | Out-Null
}
New-ItemProperty -Path $RegKeyPath -Name $LockScreenStatus -Value $StatusValue -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $RegKeyPath -Name $LockScreenPath -Value $LockScreenImageValue -PropertyType STRING -Force | Out-Null
New-ItemProperty -Path $RegKeyPath -Name $LockScreenUrl -Value $LockScreenImageValue -PropertyType STRING -Force | Out-Null
RUNDLL32.EXE USER32.DLL, UpdatePerUserSystemParameters 1, True
I pack all these files with Microsoft Win32 Content Prep Tool and upload this in Intune as Win32 file.
Upload works, assigning to a group works, installation is successful. All good so far, I thought.
If I check the directory, the image is in %AllUsersProfile%\sz.
But when I check the reg, the entry is not set.
When I run the copy.bat file manually, It doesn't work. Only when I run it as administrator.
When I run the last line in cmd as administrator, so
powershell -ExecutionPolicy Bypass -File Set-Lockscreen.ps1 -verb RunAs
It works as well.
I think Intune is not running the script as administrator.
In Intune there is no configuration to say, run this command as administrator.
Maybe with a syntax? Does anyone know this?
Something like
copy.bat RunAs
I also export the reg file and import this with
reg import PersonalizationLS.reg
It didn't work.
I think there must be a way to execute the installation command in intune to run the script as admin.
It needs just for the last line the administrator privilege , md and copy work without administrator privileges. (same also for important reg file).

Powershell back up script

So a couple of years ago I created a script that could be used and executed Windows PC that would make a backup / copy of all PDFs in a user's document folder to a flash drive regardless of username. The script also does not use the drive letter to identify the flash drive, instead, it uses the drives Volume name. So that no matter where it was used, which pc, user or letter of the flash drive, it always worked.
However now I just recreated it and for some reason, the script runs without a hitch, it is not copying the pdfs to the flash drive any longer. Any help figuring out why would be nice. I am not a pro at Powershell.
param([parameter(mandatory=$true)]$VolumeName) $backupPath = $null
get-wmiobject win32_logicaldisk | % { if ($_.VolumeName -eq $VolumeName) { $backupDrive = $_.DeviceID } }
$backupPath = $backupDrive + "\"
robocopy $env:username\Documents\ *.pdf $backupDrive /MIR /Z /XJD
Remove-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU' -Name '*' -ErrorAction SilentlyContinue
Remove-Item -path $env:username\Downloads\backup.ps1 -recurse
You create the variable $backupPath but you use the variable backupDrive in your robocopy commandline. Instead of $env:UserPorfile you use $env:userName. I doubt that this script run sucessfully once. And you have some unrelated code lines in it.
param(
[parameter(mandatory = $true)]
$VolumeName
)
$backupDrive = (Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object -Property VolumeName -EQ -Value $VolumeName).DeviceID
$backupPath = $backupDrive + '\'
robocopy $Env:USERPROFILE\Documents\ $backupPath *.pdf /S /Z /XJD /NDL /PURGE
You might have asked the author where you downloaded this script to fix it. ;-)

Copy all .jpg from subfolders to a single folder by cmd

I have a folder that has 148 folders in it, and in each of these folders they have a .jpg file. I need to get all these .jpg and put in a single folder
each folder is at least 1 .jpg
I can do this on Linux as well
`#!/bin/bash
for file in `find source -name * .jpg`;
of the mv "$ file" Destination;
done;
but I can not play this in Windows. Only the find command that I can reproduce the same result: dir /S /B *.jpg
for /r %f in (*.jpg) do move %f %destination%
Note: This is the interactive version. In a command script, you need to protect the %f's from too-early variable substitution.
for /r %%f in (*.jpg) do move %%f %destination%
Another way to do it using PowerShell. When you see that the files are being moved as you expect, remove the WhatIf from the Move-Item cmdlet.
Get-ChildItem -Recurse -File -Filter '*.jpg' |
ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }
You can run this in a cmd.exe shell or .bat script.
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Recurse -File -Filter '*.jpg' |" ^
"ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }"
The easiest way to achieve this is in Windows is to open to root folder with the Windows Explorer at which you want to start searching all the elements. Then go to the small input field for searching and enter *.jpg the result will show all the jpg in the root folder and all subfolders. Then you can simply copy and paste the files to your destination folder.

Resources