4
\$\begingroup\$

I have two different folders.

In one folder, I have files that will be exported to a remote site via FTP.

In a second folder, I have .dat files that will be processed by a ftp client program that I have. Each .dat file contains basic information that my ftp program needs to process a request.

I needed to create a bunch of dummy files and dummy requests to test my ftp program. I decided to create these dummy files with Powershell.

I know almost nothing about Powershell, so even the most basic advice would be helpful.

This is all inside a Powershell script that can be run from the Powershell ISE.

$export_src_base = "D:\csv_exports"
$export_src_dir = "$export_src_base\test"
$queue_dir = "D:\FTP\Queue"

# Create the export source directory if it does not exist. 
if (-not(Test-Path $export_src_dir)){
    New-Item -ItemType directory -Path $export_src_dir
}

for ($i=1; $i -le 100; $i++){
    # Create 100 dummy files if they do not exist.
    $file = "$i.txt"
    $filepath = "$export_src_dir\$file"

    if (-not(Test-Path $filepath)){
        New-Item -ItemType file -Path $filepath -Value $i
    }

    # Create 100 dummy requests if they do not exist.
    $queue_file = "$i.dat"
    $queue_filepath = "$queue_dir\$queue_file"

$value = @"
[Item]
SrcFileName=$filepath
DestFileName=$file
FTPSite=Site1
DestPath=Temp\Testing
Sent=0
ErrorCount=0
LastTriedTime= 
"@

    if (-not(Test-Path $queue_filepath)){
        New-Item -ItemType file -Path $queue_filepath -Value $value
    }

}
\$\endgroup\$
1
\$\begingroup\$

Pretty hard to pick on this, looks pretty nice already!

You have this duplicated pattern though:

if (-not(Test-Path $filepath)){
    New-Item -ItemType file -Path $filepath -Value $i
}
# ...
if (-not(Test-Path $queue_filepath)){
    New-Item -ItemType file -Path $queue_filepath -Value $value
}

You could create a function that takes $path and $value and use it twice instead of duplicating code.

And the comments are pointless, your code already speaks for itself (nice!), so just delete them!

Other than these nitpicks, I don't see a better way to do what you want.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ +1 Thank you. I guess learning to create functions will be the next thing I need to do. \$\endgroup\$ – jliv902 Jul 10 '14 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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