Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a script to cycle through folder and copy the folder name and save it in 'temp'. Then within that folder copy a specific file and paste it into another folder with the name 'temp.dat'

For example, take “drivers.dat” folder name, extract “drivers” Then copy ‘drivers.dat’\’dat’ file, move and rename to new_dat\drivers.dat

Thanks for the help.

Lets say the path is network path L:\Personal Folders. Over here 1) Create a “new_dat” folder 2) Cycle through the folders in the directory and: a. Take the folder name, extract the folder name without the .dat extention, save the name b. In that folder copy and rename the dat file to name_dat.dat c. Copy the new dat file to the “new_dat” folder. 3) Repeat for every folder.dat

For example, take “2.5V drivers.dat” folder name, extract “2.5V drivers” Then copy ‘2.5V drivers.dat’\’dat’ file, move and rename to new_dat\2.5 V drivers.dat

We’ll end up with a new_dat folder containing all the “dat” files but renamed as “folder name.dat".

share|improve this question
be a little more clear, I have no clue what you are wanting to do – Taylor Gibb Jan 7 '13 at 17:43
show me the folder structure and I will write a script – Taylor Gibb Jan 7 '13 at 17:44
Hi David! Please register your account here so you can edit your posts and add comments. It seems you lost your cookie, which is why I manually had to add your answer to your question. – slhck Jan 7 '13 at 22:01
Thanks, but my question was updated. I dont think your powershell script does what I wanted it to do. Is my explanation still confusing. – David Jan 7 '13 at 22:23
Basically I have a folder "Personal Folder".....within this folder I have 400 folders where all are named as "some_name.dat" Within these "some_name.dat" folders there are multiple files but there is 1 file in all the 400 folders that I am interested in extracting. The name of that file is 'dat'. What I am interested in doing is this......I made a new folder inside "Personal Folder" named "New"....I want to extract the 'dat' file from all those 400 folders and place it under "New" – David Jan 7 '13 at 22:31

Heres one written in PowerShell 3.

#Set Location to Folder
Set-Location -Path "C:\Personal Folders"

#Check If The Folder "new_dat" exists, if it doesnt, create it
if(!(Test-Path -Path "new_dat"))
    New-Item -Name "new_dat" -ItemType Directory

#Get All The Folders Except "new_dat"
$Folders = Get-ChildItem -Directory | Where-Object {$_.Name -ne "new_dat"}

#This Is Where The Magic Happens
foreach($Folder in $Folders)
    Get-ChildItem -Path $Folder.FullName -Filter *.dat | Move-Item -Destination  (Join-Path -ChildPath ([System.IO.Path]::GetFileNameWithoutExtension($Folder.Name) + "_dat.dat") -Path "C:\Personal Folders\new_dat" )
  • Install PowerShell 3
  • Put that into a text file and call it Move.ps1
  • Set up a scheduled task to make it run every X hours or whatever
share|improve this answer
Good plan. My contribution is to say make the program in Task Scheduler PowerShell and the argument -ExecutionPolicy Bypass -File "PathTo\..\Move.ps1" – Guy Thomas Jan 7 '13 at 18:04
Good catch, I always seem to take execution policies for granted. – Taylor Gibb Jan 7 '13 at 18:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.